根据定义,必须在传递之前初始化ref
关键字,而out
参数必须在从函数返回之前进行初始化。
以下是我的代码片段。
public void TestRef(ref string n)
{
}
public void TestOut(out string n)
{
n = "Hello"; //if I don't initialize, I gets compile time error. & That's right.
}
现在我们调用方法。
string name;
TestOut(out name);//fine
TestRef(ref name) // why not throwing error.
在尝试调用TestRef()时,我没有初始化name参数。但据我理解,在传递之前必须初始化ref参数。 它可以编译和运行而不会出现错误。
TestOut
的调用,name
已被初始化。 - LeeTestOut(out name)
之前执行TestRef(ref name)
- 您将会得到一个编译器错误。 - Zohar Peled