C#交換兩個變量值的多種寫法


 

在學習.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。關於異或等位運算符的介紹在這里:http://www.cnblogs.com/darrenji/p/3921183.html

 

舉例,把十進制的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();
        }

 

"橫看成嶺側成峰,遠近高低各不同",在技術世界里,可能沒有絕對的簡單或復雜,再復雜的問題可以拆分成簡單的模型;再簡單的問題,從不同的角度看待,使用不同的方式實現,就多了更多的樂趣。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM