C#基本文法之ref和out

來源:互聯網
上載者:User

標籤:輸出   方式   需要   修改   span   輸出參數   變數   lin   報錯   

ref參數必須在初始化的時候為其賦值,否則會報錯

static void Main(string[] args)        {//out需要在初始化時為變數賦值,如果初始的值為空白則會報錯            int a = 6;            int b = 66;            Fun(ref a, ref b);            Console.WriteLine("a:{0},b:{1}", a, b);            Console.ReadLine();        }        static void Fun(ref int a, ref int b)        { a= 2;            b = 1;        }

out參數在初始化時可以不賦值,但是在方法中必須初始化

 static void Main(string[] args)        {            int a =1;            int b =2;            Fun(out a, out b);            Console.WriteLine("a:{0},b:{1}", a, b);            Console.ReadLine();        }        static void Fun(out int a, out int b)        {            //a = a + b如果使用該種方式則編譯器會提示使用了未賦值的參數            a = 2;            b = 1;        }

註:ref關鍵字側重於修改參數的值,而out關鍵字側重於輸出參數值

C#基本文法之ref和out

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.