• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
红色石头
热情的血液+石头的冰冷
博客园    首页    新随笔    联系   管理    订阅  订阅
ref和out
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);
     }
}
posted on 2006-08-12 00:54  红色石头  阅读(209)  评论(3)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3