通过一段代码说明C#中rel与out的使用区别

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    } 
17
18    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);
28
29        //ref test 
30        int m, n;
31        //refTest(ref m, ref n); 
32        //上面这行会出错,ref使用前,变量必须赋值 
33
34        int o = 11, p = 22;
35        refTest(ref o, ref p);
36        Response.Write("o={11};p={22}" + o + p);
37
38
39    }
40}

1.ref 有进有出,使用前需实例化

2.out只出不进(即便已经实例化参数,调用函数时,依旧为null),

原文地址:https://www.cnblogs.com/slu182/p/4256407.html