[C.C++] C#交换两个变量值的几种方法总结

1912 0
Honkers 2022-11-5 08:27:43 | 显示全部楼层 |阅读模式
在学习.Net/C#或者任何一门面向对象语言的初期,大家都写过交换两个变量值,通常是通过临时变量来实现。本篇使用多种方式实现两个变量值的交换。
假设int x =1; int y = 2;现在交换两个变量的值。
使用临时变量实现
  1.         static void Main(string[] args)
  2.         {
  3.             int x = 1;
  4.             int y = 2;
  5.             Console.WriteLine("x={0},y={1}",x, y);
  6.             int temp = x;
  7.             x = y;
  8.             y = temp;
  9.             Console.WriteLine("x={0},y={1}", x, y);
  10.             Console.ReadKey();
  11.         }
复制代码
使用加减法实现

试想, 1+2=3,我们得到了两数相加的结果3。3-2=1,把1赋值给y,y就等于1; 3-1=2,把2赋值给x,这就完成了交换。
  1.         static void Main(string[] args)
  2.         {
  3.             int x = 1;
  4.             int y = 2;
  5.             Console.WriteLine("x={0},y={1}",x, y);
  6.             x = x + y; //x = 3
  7.             y = x - y; //y = 1
  8.             x = x - y; //x = 2
  9.             Console.WriteLine("x={0},y={1}", x, y);
  10.             Console.ReadKey();
  11.         }
复制代码
使用ref和泛型方法实现

如果把交换int类型变量值的算法封装到方法中,需要用到ref关键字。
  1.         static void Main(string[] args)
  2.         {
  3.             int x = 1;
  4.             int y = 2;
  5.             Console.WriteLine("x={0},y={1}",x, y);
  6.             Swap(ref x, ref  y);
  7.             Console.WriteLine("x={0},y={1}", x, y);
  8.             Console.ReadKey();
  9.         }
  10.         static void Swap(ref int x, ref int y)
  11.         {
  12.             int temp = x;
  13.             x = y;
  14.             y = x;
  15.         }
复制代码
如果交换string类型的变量值,就要写一个Swap方法的重载,让其接收string类型:
  1.         static void Main(string[] args)
  2.         {
  3.             string x = "hello";
  4.             string y = "world";
  5.             Console.WriteLine("x={0},y={1}",x, y);
  6.             Swap(ref x, ref  y);
  7.             Console.WriteLine("x={0},y={1}", x, y);
  8.             Console.ReadKey();
  9.         }
  10.         static void Swap(ref int x, ref int y)
  11.         {
  12.             int temp = x;
  13.             x = y;
  14.             y = x;
  15.         }
  16.         static void Swap(ref string x, ref string y)
  17.         {
  18.             string temp = x;
  19.             x = y;
  20.             y = x;
  21.         }
复制代码
如果交换其它类型的变量值呢?我们很容易想到通过泛型方法来实现,再写一个泛型重载。
  1.         static void Main(string[] args)
  2.         {
  3.             string x = "hello";
  4.             string y = "world";
  5.             Console.WriteLine("x={0},y={1}",x, y);
  6.             Swap<string>(ref x, ref y);
  7.             Console.WriteLine("x={0},y={1}", x, y);
  8.             Console.ReadKey();
  9.         }
  10.         static void Swap(ref int x, ref int y)
  11.         {
  12.             int temp = x;
  13.             x = y;
  14.             y = x;
  15.         }
  16.         static void Swap(ref string x, ref string y)
  17.         {
  18.             string temp = x;
  19.             x = y;
  20.             y = x;
  21.         }
  22.         static void Swap<T>(ref T x, ref T y)
  23.         {
  24.             T temp = x;
  25.             x = y;
  26.             y = temp;
  27.         }
复制代码
使用按位异或运算符实现

对于二进制数字来说,当两个数相异的时候就为1, 即0和1异或的结果是1, 0和0,以及1和1异或的结果是0。关于异或等位运算符的介绍在这里:https://www.jb51.net/article/260847.htm
举例,把十进制的3和4转换成16位二进制分别是:
x = 0000000000000011;//对应十进制数字3
y = 0000000000000100; //对应十进制数字4
把x和y异或的结果赋值给x:x = x ^ y;
x = 0000000000000111;
把y和现在的x异或,结果赋值给y:y = y ^ x
y = 0000000000000011;
把现在的x和现在的y异或,结果赋值给x:x = x ^ y
x = 0000000000000100;
按照上面的算法,可以写成如下:
  1.         static void Main(string[] args)
  2.         {
  3.             int x = 1;
  4.             int y = 2;
  5.             Console.WriteLine("x={0},y={1}",x, y);
  6.             x = x ^ y;
  7.             y = y ^ x;
  8.             x = x ^ y;
  9.             Console.WriteLine("x={0},y={1}", x, y);
  10.             Console.ReadKey();
  11.         }
复制代码
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对中国红客联盟的支持。如果你想了解更多相关内容请查看下面相关链接
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Honkers

荣誉红客

关注
  • 4008
    主题
  • 36
    粉丝
  • 0
    关注
这家伙很懒,什么都没留下!

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行