To swap two variables in a single line using the Bitwise XOR Operator.
val1 = val1 ^ val2 ^ (val2 = val1);
Above, we have set the values −
int val1 = 30; int val2 = 60;
以下是使用C#一行程式碼交換變數的範例:
using System; class Demo { public static void Main(String[] args) { int val1 = 30; int val2 = 60; Console.WriteLine("Values before swap"); Console.WriteLine(val1); Console.WriteLine(val2); val1 = val1 ^ val2 ^ (val2 = val1); Console.WriteLine("Values after swap"); Console.WriteLine(val1); Console.WriteLine(val2); } }
以上是使用 C# 在一行中交換兩個變數的詳細內容。更多資訊請關注PHP中文網其他相關文章!