通過一段代碼說明C#中rel與out的使用區別

來源:互聯網
上載者:User

標籤:

using System; 2 3public partial class testref : System.Web.UI.Page 4{ 5    static void outTest(out int x, out int y) 6    {//離開這個函數前,必須對x和y賦值,否則會報錯。  7        //y = x;  8        //上面這行會報錯,因為使用了out後,x和y都清空了,需要重新賦值,即使調用函數前賦過值也不行  9        x = 1;10        y = 2;11    }12    static void refTest(ref int x, ref int y)13    {14        x =x+ 1;15        y = y+1;16    } 1718    protected void Page_Load(object sender, EventArgs e)19    {20        //out test 21        int a, b;22        //out使用前,變數可以不賦值 23        outTest(out a, out b);24        Response.Write("a={0};b={1}"+a+b);25        int c = 11, d = 22;26        outTest(out c, out d);27        Response.Write("c={0};d={1}"+c+d);2829        //ref test 30        int m, n;31        //refTest(ref m, ref n); 32        //上面這行會出錯,ref使用前,變數必須賦值 3334        int o = 11, p = 22;35        refTest(ref o, ref p);36        Response.Write("o={11};p={22}" + o + p);373839    }40}

1.ref 有進有出,使用前需執行個體化

2.out只出不進(即便已經執行個體化參數,調用函數時,依舊為null),

通過一段代碼說明C#中rel與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.