C# out Keyword

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 }

 

posted @ 2016-09-14 02:26  Dylan_Java_NYC  阅读(216)  评论(0编辑  收藏  举报