根据定义,ref
关键字必须在传递之前进行初始化。而out
参数必须在从函数返回之前进行初始化。为什么我没有得到与ref关键字错误
下面是我的代码片段。
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'已通过调用'TestOut'进行初始化。 – Lee
尝试在TestOut(out name)之前执行'TestRef(ref name)' - 你会得到一个编译器错误。 –
@李。你是对的。我明白了。愚蠢的错误。谢谢 –