传递到 ref 参数的参数必须最先初始化。
这与 out 不同,out 的参数在传递之前不需要显式初始化。
class RefRefExample
{
static void Method(ref string s)
{
s = "changed ";
}
static void Main()
{
string str = "original ";
Method(ref str);
// str is now "changed "
}
}
class OutReturnExample
{
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;
}
}