Compiler Error CS0269
使用了未赋值的 out 参数“parameter”
编译器未能验证在使用 out 参数之前是否已给它赋值,它的值在赋值时可能未定义。在访问值之前,请确保为调用的方法中的 out 参数赋值。如果您需要使用传入的变量的值,请改用 ref 参数。有关更多信息,请参见传递参数(C# 编程指南)。
下面的示例生成 CS0269:
// CS0269.cs
class C
{
public static void F(out int i)
// One way to resolve the error is to use a ref parameter instead
// of an out parameter.
//public static void F(ref int i)
{
// The following line causes a compiler error because no value
// has been assigned to i.
int k = i; // CS0269
i = 1;
// The error does not occur if the order of the two previous
// lines is reversed.
}
public static void Main()
{
int myInt = 1;
F(out myInt);
// If the declaration of method F is changed to require a ref
// parameter, ref must be specified in the call as well.
//F(ref myInt);
}
}
如果变量的初始化发生在 try 块中,则也会出现此错误,因为编译器无法验证 try 块是否可以成功执行:
// CS0269b.cs
class C
{
public static void F(out int i)
{
try
{
// Assignment occurs, but compiler can't verify it
i = 1;
}
catch
{
}
int k = i; // CS0269
i = 1;
}
public static void Main()
{
int myInt;
F(out myInt);
}
}