[C#] Swapping the Values of Two Variables
발행일: Jan, 2025
조회수: 0
단어수: 45
1. Classic Method
Declare an additional temporary variable, copy the value or reference of one of the two variables into it, and then perform the swap.
var a = 10;
var b = 20;
var temp = a;
a = b;
b = temp;
2. Modern Method (C# 7.0 or later)
Using a tuple, you can achieve the swap in a single line.
(b, a) = (a, b)