c#編程基礎之ref、out參數

來源:互聯網
上載者:User

標籤:style   blog   http   color   使用   io   資料   2014   

引例:

先看這個源碼,函數傳遞後由於傳遞的是副本所以真正的值並沒有改變。

源碼如下:

using System;using System.Collections.Generic;using System.Text;namespace refout參數學習{    class Program    {        static void Main(string[] args)        {            int age = 20;            IncAge(age);            Console.WriteLine(age);//列印結果還是20            Console.ReadKey();                    }        static void IncAge(int age)//複製了一份,所以IncAge內部改變的是副本        {            age++;        }    }}

運行:

 

要解決上面方法就需要使用ref參數:

上面源碼修改後如下:

using System;using System.Collections.Generic;using System.Text;namespace refout參數學習{    class Program    {        static void Main(string[] args)        {            int age = 20;            IncAge(ref age);//此處必須也加上參數ref            Console.WriteLine(age);//使用參數ref後,傳遞真值,而不是副本,所以列印21            Console.ReadKey();                    }        static void IncAge(ref int age)//使用ref參數後,傳遞過來的將不是副本,而是原本,函數內改變,其值也將發生改變        {            age++;        }    }}

程式:

out參數由內部進行賦值,所傳遞參數無需初始化,而且即使初始化也沒用。

源碼如下:

using System;using System.Collections.Generic;using System.Text;namespace refout參數學習{    class Program    {        static void Main(string[] args)        {            int age;            IncAge(out age);//此處必須也加上參數out            Console.WriteLine(age);//使用參數out後,傳遞真值,並有函數內部賦值所以列印22            Console.ReadKey();                    }        static void IncAge(out int age)//使用out參數後,所傳遞值不需要初始化,由函數內部為其賦值。        {            age = 22;        }    }}

程式:

out參數應用情境:

源碼如下:

using System;using System.Collections.Generic;using System.Text;namespace refout參數學習{    class Program    {        static void Main(string[] args)        {            string str = Console.ReadLine();            int i;            if(int.TryParse(str,out i))            {                Console.WriteLine("轉換成功!{0}",i);            }            else            {                Console.WriteLine("資料錯誤!");            }            Console.ReadKey();        }           }}

運行結果:

ref運行情境,比如我們先看這個源碼:

using System;using System.Collections.Generic;using System.Text;namespace refout參數學習{    class Program    {        static void Main(string[] args)        {            int i1 = 10;            int i2 = 20;            Swap(i1, i2);//將i1和i2各複製一份傳遞給函數            Console.WriteLine("i1={0},i2={1}", i1, i2);//i1和i2沒有發生交換,還是i1=10,i2=20                   Console.ReadKey();        }        static void Swap(int i1, int i2) //交換函數        {            int temp=i1;            i1 = i2;            i2 = temp;        }           }}

運行結果:

想要實現真的交換,這時就需要ref參數了,修改源碼如下:

using System;using System.Collections.Generic;using System.Text;namespace refout參數學習{    class Program    {        static void Main(string[] args)        {            int i1 = 10;            int i2 = 20;            Swap(ref i1,ref i2);//將i1和i2傳遞給函數            Console.WriteLine("i1={0},i2={1}", i1, i2);//i1和i2發生交換,還是i1=20,i2=10                 Console.ReadKey();        }        static void Swap(ref int i1,ref int i2) //接受傳遞來的i1和i2的值,將i1和i2的值進行交換。        {            int temp=i1;            i1 = i2;            i2 = temp;        }           }}

程式:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.