Swap variables elegantly in C#
Approach
Classic way
var a = 10;
var b = 20;
var aux = a;
a = b;
b = aux;
With tuple
var a = 10;
var b = 20;
(a, b) = (b, a);
Benchmark
For primitive types
For classes
Conclusion
In both cases the performance difference is not measurable.
The only difference is that the tuple approach is more readable and less error prone. So, use it! 🤓