欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#交換兩個(gè)變量值的幾種方法總結(jié)

 更新時(shí)間:2022年10月22日 12:02:58   作者:Darren Ji  
這篇文章介紹了C#交換兩個(gè)變量值的幾種方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

在學(xué)習(xí).Net/C#或者任何一門面向?qū)ο笳Z言的初期,大家都寫過交換兩個(gè)變量值,通常是通過臨時(shí)變量來實(shí)現(xiàn)。本篇使用多種方式實(shí)現(xiàn)兩個(gè)變量值的交換。

假設(shè)int x =1; int y = 2;現(xiàn)在交換兩個(gè)變量的值。

使用臨時(shí)變量實(shí)現(xiàn)

        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();
        }

使用加減法實(shí)現(xiàn)

試想, 1+2=3,我們得到了兩數(shù)相加的結(jié)果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和泛型方法實(shí)現(xiàn)

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

        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類型的變量值,就要寫一個(gè)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;
        }

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

        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;
        }

使用按位異或運(yùn)算符實(shí)現(xiàn)

對(duì)于二進(jìn)制數(shù)字來說,當(dāng)兩個(gè)數(shù)相異的時(shí)候就為1, 即0和1異或的結(jié)果是1, 0和0,以及1和1異或的結(jié)果是0。關(guān)于異或等位運(yùn)算符的介紹在這里:http://www.dbjr.com.cn/article/260847.htm

舉例,把十進(jìn)制的3和4轉(zhuǎn)換成16位二進(jìn)制分別是:

x = 0000000000000011;//對(duì)應(yīng)十進(jìn)制數(shù)字3
y = 0000000000000100; //對(duì)應(yīng)十進(jìn)制數(shù)字4

把x和y異或的結(jié)果賦值給x:x = x ^ y;
x = 0000000000000111;

把y和現(xiàn)在的x異或,結(jié)果賦值給y:y = y ^ x
y = 0000000000000011;

把現(xiàn)在的x和現(xiàn)在的y異或,結(jié)果賦值給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();
        }

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接

相關(guān)文章

最新評(píng)論