首页 > Windows开发 > 详细

C# out Keyword

时间:2016-09-14 07:11:35      阅读:159      评论:0      收藏:0      [点我收藏+]

In C#, out keyword 是argument传值变成passed by reference. out keyword 在同时返回多个值时很有用.

与ref keyword 相似. 若是使用out keyword传argument, 那么在method 的definition 和 使用时都需要家out keyword. 

Async methods can‘t use out keyword.

 

Differences between out keyword and ref keyword:

ref requires that variable be initialized before it is passed.

 1 class OutReturnExample
 2 {
 3     static void Method(out int i, out string s1, out string s2)
 4     {
 5         i = 44;
 6         s1 = "I‘ve been returned";
 7         s2 = null;
 8     }
 9     static void Main()
10     {
11         int value;
12         string str1, str2;
13         Method(out value, out str1, out str2);
14         // value is now 44
15         // str1 is now "I‘ve been returned"
16         // str2 is (still) null;
17     }
18 }

 

C# out Keyword

原文:http://www.cnblogs.com/Dylan-Java-NYC/p/5870451.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!