ref 和 out 值类型变量
ref and out with value type variables
msdn documentation on out 表示作为 out 传递的参数必须在函数内部赋值。来自网站的示例:
class OutExample
{
static void Method(out int i)
{
i = 44;
}
static void Main()
{
int value;
Method(out value);
// value is now 44
}
}
根据我的理解,当声明 int "value" 时,它已经被分配了一个默认值 0(因为 int 是一个值类型,不能为 null。)那么为什么 "Method"修改它的值?
同理,如果不使用out而使用"ref",是否需要初始化"value"?
有这样的问题What's the difference between the 'ref' and 'out' keywords?但是没有人想把2和2放在一起[=13=]
默认情况下不会为 int 分配值,即使有默认值...试试这个:
Console.WriteLine(default(int)); // output = '0'
int foo;
Console.WriteLine(foo); // compile error - Use of unassigned local variable
out 参数的约定是保证方法为其赋值。 ref 参数的合约没有这样的保证。
您必须为变量赋值。但是您在 post 中提到的 out
参数的概念具有更多的语义含义。
如果您传递 out
参数,您希望您调用的函数必须对其进行分配。 C#
编译器只是强制执行 "good programmer" 编译时间控制技能,以获得更好的代码风格。
According to my understanding when the int "value" is declared it is
already assigned a default value of 0 (since int is a value type and
cannot be null.)
不,那是不正确的。
局部变量在创建时没有被赋值。局部变量的 space 是通过移动堆栈指针为它们在堆栈上腾出空间来创建的。该内存区域未被清除,它将包含恰好存在的所有值。
编译器会强制您在变量可以使用之前为其赋值,因此它最初包含的 "garbage" 值永远不会被使用。
Similarly, if "ref" were to be used instead of out, would there be any
need of initializing "value"?
不需要在方法中设置该值,因为它已经有一个值。用于调用该方法的变量需要初始化:
static void Method(ref int i) {
// doesn't need to set the value
}
static void Main() {
int value;
value = 42; // needs to be initialised before the call
Method(ref value);
// value is still 42
}
有时您的方法需要 return 多个值,因此在这种情况下,您可以通过 ref 和 out 参数 return 这些额外值。 .NET 框架中的 "TryParse" 方法是对这些的一个很好的参考,例如 "Int32.TryParse Method"
[1]: https://msdn.microsoft.com/en-us/library/f02979c7%28v=vs.110%29.aspx
您可以在 MSDN 中找到有关上述 link 的更多信息。
msdn documentation on out 表示作为 out 传递的参数必须在函数内部赋值。来自网站的示例:
class OutExample
{
static void Method(out int i)
{
i = 44;
}
static void Main()
{
int value;
Method(out value);
// value is now 44
}
}
根据我的理解,当声明 int "value" 时,它已经被分配了一个默认值 0(因为 int 是一个值类型,不能为 null。)那么为什么 "Method"修改它的值?
同理,如果不使用out而使用"ref",是否需要初始化"value"?
有这样的问题What's the difference between the 'ref' and 'out' keywords?但是没有人想把2和2放在一起[=13=]
默认情况下不会为 int 分配值,即使有默认值...试试这个:
Console.WriteLine(default(int)); // output = '0'
int foo;
Console.WriteLine(foo); // compile error - Use of unassigned local variable
out 参数的约定是保证方法为其赋值。 ref 参数的合约没有这样的保证。
您必须为变量赋值。但是您在 post 中提到的 out
参数的概念具有更多的语义含义。
如果您传递 out
参数,您希望您调用的函数必须对其进行分配。 C#
编译器只是强制执行 "good programmer" 编译时间控制技能,以获得更好的代码风格。
According to my understanding when the int "value" is declared it is already assigned a default value of 0 (since int is a value type and cannot be null.)
不,那是不正确的。
局部变量在创建时没有被赋值。局部变量的 space 是通过移动堆栈指针为它们在堆栈上腾出空间来创建的。该内存区域未被清除,它将包含恰好存在的所有值。
编译器会强制您在变量可以使用之前为其赋值,因此它最初包含的 "garbage" 值永远不会被使用。
Similarly, if "ref" were to be used instead of out, would there be any need of initializing "value"?
不需要在方法中设置该值,因为它已经有一个值。用于调用该方法的变量需要初始化:
static void Method(ref int i) {
// doesn't need to set the value
}
static void Main() {
int value;
value = 42; // needs to be initialised before the call
Method(ref value);
// value is still 42
}
有时您的方法需要 return 多个值,因此在这种情况下,您可以通过 ref 和 out 参数 return 这些额外值。 .NET 框架中的 "TryParse" 方法是对这些的一个很好的参考,例如 "Int32.TryParse Method"
[1]: https://msdn.microsoft.com/en-us/library/f02979c7%28v=vs.110%29.aspx
您可以在 MSDN 中找到有关上述 link 的更多信息。