【发布时间】:2017-06-08 00:18:02
【问题描述】:
根据定义,ref关键字必须在传递之前进行初始化。而out参数必须在从函数返回之前进行初始化。
下面是我的sn-p。
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 参数必须在传递之前初始化。
它可以毫无错误地构建和运行。
【问题讨论】:
-
name已通过对TestOut的调用初始化。 -
尝试在
TestOut(out name)之前执行TestRef(ref name)- 你会得到一个编译器错误。 -
@Lee。你说得对。我明白了。愚蠢的错误。谢谢
-
@ZoharPeled。是的。谢谢