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中文网其他相关文章!