【发布时间】:2015-04-14 19:22:56
【问题描述】:
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“值”时,它已经被分配了一个默认值0(因为int是一种值类型,不能为空。)那么为什么“方法”有必要修改它的值?
同样,如果用“ref”代替out,是否需要初始化“value”?
有这样的问题What's the difference between the 'ref' and 'out' keywords?,但没有人愿意把 2 和 2 放在一起。
【问题讨论】: