日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C#交換兩個變量值的幾種方法總結_C#教程

作者:Darren?Ji ? 更新時間: 2022-11-29 編程語言

在學習.Net/C#或者任何一門面向對象語言的初期,大家都寫過交換兩個變量值,通常是通過臨時變量來實現。本篇使用多種方式實現兩個變量值的交換。

假設int x =1; int y = 2;現在交換兩個變量的值。

使用臨時變量實現

        static void Main(string[] args)
        {
            int x = 1;
            int y = 2;
            Console.WriteLine("x={0},y={1}",x, y);
            int temp = x;
            x = y;
            y = temp;
            Console.WriteLine("x={0},y={1}", x, y);
            Console.ReadKey();
        }

使用加減法實現

試想, 1+2=3,我們得到了兩數相加的結果3。3-2=1,把1賦值給y,y就等于1; 3-1=2,把2賦值給x,這就完成了交換。

        static void Main(string[] args)
        {
            int x = 1;
            int y = 2;
            Console.WriteLine("x={0},y={1}",x, y);
            x = x + y; //x = 3
            y = x - y; //y = 1
            x = x - y; //x = 2 
            Console.WriteLine("x={0},y={1}", x, y);
            Console.ReadKey();
        }

使用ref和泛型方法實現

如果把交換int類型變量值的算法封裝到方法中,需要用到ref關鍵字。

        static void Main(string[] args)
        {
            int x = 1;
            int y = 2;
            Console.WriteLine("x={0},y={1}",x, y);
            Swap(ref x, ref  y);
            Console.WriteLine("x={0},y={1}", x, y);
            Console.ReadKey();
        }
        static void Swap(ref int x, ref int y)
        {
            int temp = x;
            x = y;
            y = x;
        }

如果交換string類型的變量值,就要寫一個Swap方法的重載,讓其接收string類型:

        static void Main(string[] args)
        {
            string x = "hello";
            string y = "world";
            Console.WriteLine("x={0},y={1}",x, y);
            Swap(ref x, ref  y);
            Console.WriteLine("x={0},y={1}", x, y);
            Console.ReadKey();
        }
        static void Swap(ref int x, ref int y)
        {
            int temp = x;
            x = y;
            y = x;
        }
        static void Swap(ref string x, ref string y)
        {
            string temp = x;
            x = y;
            y = x;
        }

如果交換其它類型的變量值呢?我們很容易想到通過泛型方法來實現,再寫一個泛型重載。

        static void Main(string[] args)
        {
            string x = "hello";
            string y = "world";
            Console.WriteLine("x={0},y={1}",x, y);
            Swap<string>(ref x, ref y);
            Console.WriteLine("x={0},y={1}", x, y);
            Console.ReadKey();
        }
        static void Swap(ref int x, ref int y)
        {
            int temp = x;
            x = y;
            y = x;
        }
        static void Swap(ref string x, ref string y)
        {
            string temp = x;
            x = y;
            y = x;
        }
        static void Swap<T>(ref T x, ref T y)
        {
            T temp = x;
            x = y;
            y = temp;
        }

使用按位異或運算符實現

對于二進制數字來說,當兩個數相異的時候就為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;

按照上面的算法,可以寫成如下:

        static void Main(string[] args)
        {
            int x = 1;
            int y = 2;
            Console.WriteLine("x={0},y={1}",x, y);
            x = x ^ y;
            y = y ^ x;
            x = x ^ y;
            Console.WriteLine("x={0},y={1}", x, y);
            Console.ReadKey();
        }

原文鏈接:https://www.cnblogs.com/darrenji/p/4464799.html

欄目分類
最近更新