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

C# out Keyword

时间:2016-09-14 07:11:52      阅读:179      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!