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

C# ref和out传递参数总结

时间:2015-02-16 00:23:04      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:

如有雷同,不胜荣幸,若转载,请注明

C#中ref和out传递参数总结,两个都可用来传递参数,ref使用时必须先进行初始化,out则不需要,只要在返回之前赋值即可,文字废话到此,下面直接上例子

ref例子

Class A

{

private string name = string.Empty;

private int count = 0;

...

GetName(ref name,ref count);

Console.Write(“姓名:” + name + ",数量:" + count);

}

Class B

{

...

 

//参数name是1个指向堆栈中值类型为string的指针
      //参数count是1个指向堆栈中值类型为int的指针

 

public  bool GetName(ref string name,ref int count)

{

...

name = "ching";

count = 10;

return true;

}

}

输出结果:姓名:ching,数量:10

out例子

 

Class A

{

private string name;

private int count;

...

GetName(ref name,ref count);

Console.Write(“姓名:” + name + ",数量:" + count);

}

Class B

{

...

public  bool GetName(ref string name,ref int count)

{

...

name = "ching";

count = 10;

return true;

}

}

同样这个输出结果:姓名:ching,数量:10

综上所述,ref和out在C#中有着相同的功效,只是注意ref需要先初始化值,out则不需要,但是要在返回之前赋一个值,以上所说是简单阐述,详细介绍或者在使用上有疑惑之处请查看官方原版msdn手册即可,欢迎大家斧正

C# ref和out传递参数总结

标签:

原文地址:http://www.cnblogs.com/ching2009/p/4293590.html

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