码迷,mamicode.com
首页 > Windows程序 > 详细

c# 基本知识 ref 和 out

时间:2015-09-24 21:20:13      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:

一:首先两者都是引用传递,都是按地址传递的。

二: 区别

(1)ref 默认必须初始化,out 不需要显式初始化。例如:

     

1   int num1=10;
2 
3        int num2;
4 
5       refMethod(ref num1);
6 
7       outMethod(out num2);

 

 (2)必须在方法返回之前为 out 参数赋值,ref则不是必须的,例如:

  

1  public static void refMethod(ref int num)
2         {
3             num = 30; //不是必须的
4         }
5         public static void outMethod(out int num)
6         {
7             num = 8; //必须在方法返回之前赋值
8         }

或者说ref是有进有出,out是只出不进 。

 

c# 基本知识 ref 和 out

标签:

原文地址:http://www.cnblogs.com/sunyj/p/4836478.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!