您的位置:首页 > 编程语言 > C#

C# ref and out

2007-04-03 13:56 387 查看
pass parameters with reference:

ref and out same:

both the method definition and the calling method must explicitly use the ref keyword.

both the method definition and the calling method must explicitly use the out keyword

static void Method(out int i, out string s1, out string s2)
{
i = 44;
s1 = "I've been returned";
s2 = null;
}
static void Main()
{
int value;
string str1, str2;
Method(out value, out str1, out str2);
// value is now 44
// str1 is now "I've been returned"
// str2 is (still) null;
}

Different:

An argument passed to a ref parameter must first be initialized. This differs from out, whose argument need not be explicitly initialized before being passed.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: