在C#中out保留字怎么使用

表示这个变量要回传值,最简单的应用是除法,比如你需要一个除法方法,同时得到余数和商,但是普通的方法只能得到一个返回值,这个时候就可以使用Out参数,把另一个值返回。
比如,你定义了一个方法int a(int b,out int c),它除了能得到返回值外,还可以在方法里对C进行赋值,这样你就可以使用C的值了。

①ref要求参数必须被初始化,out没有这个要求。  
  ②ref参数可以被修改,也可以不被修改,但是out必须被赋值或者修改。  
  ③ref参数值可以在任何时候被调用参数使用,而out参数值只能在被赋值后才可以被使用。

1,明确语义:out在调用函数里必须赋值,语法检查里能确保这个变量确实被函数修改了。  
  2,相关性:out在调用函数前的赋值是被忽略的,也就是说与方法调用前的值无关,所以只要声明过就可以了。   

方法参数上的 out 方法参数关键字使方法引用传递到方法的同一个变量。
当控制传递回调用方法时,在方法中对参数所做的任何更改都将反映在该变量中。
当希望方法返回多个值时,声明 out 方法非常有用。
使用 out 参数的方法仍然可以返回一个值。一个方法可以有一个以上的 out 参数。
若要使用 out 参数,必须将参数作为 out 参数显式传递到方法。out 参数的值不会传递到 out 参数。
不必初始化作为 out 参数传递的变量。然而,必须在方法返回之前为 out 参数赋值。
属性不是变量,不能作为 out 参数传递。

方法参数上的 ref 方法参数关键字使方法引用传递到方法的同一个变量。
当控制传递回调用方法时,在方法中对参数所做的任何更改都将反映在该变量中。
若要使用 ref 参数,必须将参数作为 ref 参数显式传递到方法。
ref 参数的值被传递到 ref 参数。 传递到 ref 参数的参数必须最先初始化。
将此方法与 out 参数相比,后者的参数在传递到 out 参数之前不必显式初始化。
属性不是变量,不能作为 ref 参数传递。

两者都是按地址传递的,使用后都将改变原来的数值。
ref可以把参数的数值传递进函数,但是out是要把参数清空
就是说你无法把一个数值从out传递进去的,out进去后,参数的数值为空,所以你必须初始化一次。
两个的区别:ref是有进有出,out是只出不进。

代码实例如下:

 1  namespace  TestOutAndRef
 2  {
 3       class  TestApp
 4      {
 5 
 6    static   void  outTest( out   int  x,  out   int  y)
 7   { // 离开这个函数前,必须对x和y赋值,否则会报错。
 8     // y = x;
 9     // 上面这行会报错,因为使用了out后,x和y都清空了,需要重新赋值,即使调用函数前赋过值也不行
10    x  =   1 ;
11    y  =   2 ;
12   }
13    static   void  refTest( ref   int  x,  ref   int  y)
14   {
15    x  =   1 ;
16    y  =  x;
17   }
18 
19 
20    static   public   void  OutArray( out   int [] myArray)
21   {
22        //  Initialize the array:
23       myArray  =   new   int [ 5 ] {  1 2 3 4 5  };
24   }
25    public   static   void  FillArray( ref   int [] arr)
26   {
27        //  Create the array on demand:
28        if  (arr  ==   null )
29           arr  =   new   int [ 10 ];
30        //  Otherwise fill the array:
31       arr[ 0 =   123 ;
32       arr[ 4 =   1024 ;
33   }
34 
35 
36    public   static   void  Main()
37   {
38     // out test
39     int  a,b;
40     // out使用前,变量可以不赋值
41    outTest( out  a,  out  b);
42    Console.WriteLine( " a={0};b={1} " ,a,b);
43     int  c = 11 ,d = 22 ;
44    outTest( out  c,  out  d);
45    Console.WriteLine( " c={0};d={1} " ,c,d);
46 
47     // ref test
48     int  m,n;
49     // refTest(ref m, ref n);
50     // 上面这行会出错,ref使用前,变量必须赋值
51 
52     int  o = 11 ,p = 22 ;
53    refTest( ref  o,  ref  p);
54    Console.WriteLine( " o={0};p={1} " ,o,p);
55 
56 
57 
58     int [] myArray1;  //  Initialization is not required
59 
60     //  Pass the array to the callee using out:
61    OutArray( out  myArray1);
62 
63     //  Display the array elements:
64    Console.WriteLine( " Array1 elements are: " );
65     for  ( int  i  =   0 ; i  <  myArray1.Length; i ++ )
66        Console.WriteLine(myArray1[i]);
67 
68     //  Initialize the array:
69     int [] myArray  =  {  1 2 3 4 5  };
70 
71     //  Pass the array using ref:
72    FillArray( ref  myArray);
73 
74     //  Display the updated array:
75    Console.WriteLine( " Array elements are: " );
76     for  ( int  i  =   0 ; i  <  myArray.Length; i ++ )
77        Console.WriteLine(myArray[i]);
78   }
79      }
80 
81  }

运行结果 如下:
   


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值