ref和out

       共同点:方法参数上的 ref 和out方法参数关键字使方法引用传递到方法的同一个变量。当控制传递回调用方法时,在方法中对参数所做的任何更改都将反映在该变量中。
       不同点:传递到 ref 参数的变量必须最先初始化。传递到 out 参数的变量不必初始化,然而,必须在方法返回之前为 out 参数赋值。
        注意:无法定义仅在 ref 和 out 方面不同的重载,以下重载声明是无效的:
  class MyClass
 { 
        public void MyMethod(out int i) {i = 10;}
        public void MyMethod(ref int i) {i = 10;}
}
当希望方法返回多个值时,声明 out 方法非常有用。
示例
// cs_out.cs
 using System;
 public class MyClass
 {
     public static int TestOut(out char i,out char j)
     {
         i = 'b';
         j = 'c'; 
        return -1;
     }
     public static void Main()
     {
         char i; // variable need not be initialized char j; 
        Console.WriteLine(TestOut(out i,out j)); 
        Console.WriteLine(i);
         Console.WriteLine(j);
     }
}

转载于:https://www.cnblogs.com/WhiteCloud/archive/2006/08/12/474798.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值