C#“out”的值何时为?或“参考”参数实际上返回给调用者?
当我对 out
或 ref
参数进行赋值时,值是立即分配给调用者提供的引用,还是 out
和 ref 参数值在方法返回时分配给引用?如果方法抛出异常,是否返回值?
例如:
int callerOutValue = 1;
int callerRefValue = 1;
MyMethod(123456, out callerOutValue, ref callerRefValue);
bool MyMethod(int inValue, out int outValue, ref int refValue)
{
outValue = 2;
refValue = 2;
throw new ArgumentException();
// Is callerOutValue 1 or 2?
// Is callerRefValue 1 or 2?
}
When I make an assignment to an out
or ref
parameter, is the value immediately assigned to the reference provided by the caller, or are the out
and ref
parameter values assigned to the references when the method returns? If the method throws an exception, are the values returned?
For example:
int callerOutValue = 1;
int callerRefValue = 1;
MyMethod(123456, out callerOutValue, ref callerRefValue);
bool MyMethod(int inValue, out int outValue, ref int refValue)
{
outValue = 2;
refValue = 2;
throw new ArgumentException();
// Is callerOutValue 1 or 2?
// Is callerRefValue 1 or 2?
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于
ref
和out
参数允许方法使用调用者传入的实际引用,因此当控制权返回时,对这些引用的所有更改都会立即反映给调用者。这意味着在上面的示例中(当然,如果您要捕获
ArgumentException
),outValue
和refValue
都将设置为 2。还需要注意的是,
out
和ref
在 IL 级别是相同的概念 - 只有 C# 编译器强制执行out
的额外规则> 这要求方法在返回之前设置其值。因此,从 CLR 角度来看,outValue
和refValue
具有相同的语义,并且以相同的方式处理。Since
ref
andout
parameters allow a method to work with the actual references that the caller passed in, all changes to those references are reflected immediately to the caller when control is returned.This means in your example above (if you were to catch the
ArgumentException
of course),outValue
andrefValue
would both be set to 2.It is also important to note that
out
andref
are identical concepts at an IL level - it is only the C# compiler that enforces the extra rule forout
that requires that a method set its value prior to returning. So from an CLR perspectiveoutValue
andrefValue
have identical semantics and are treated the same way.安德鲁是正确的;我只会添加一些额外的细节。
首先,考虑 out/ref 参数的正确方法是它们是变量的别名。也就是说,当您有一个方法 M(ref int q) 并将其称为 M(ref x) 时,q 和 x 是完全相同的变量的两个不同名称。变量是一个存储位置;你在 q 中存储一些东西,你也将它存储在 x 中,因为它们是同一位置的两个不同名称。
其次,您描述的替代方案称为“复制入/复制出”引用。在此方案中,有两个存储位置,其中一个存储位置的内容在函数调用开始时复制到其中,并在函数调用完成时复制回来。正如您所注意到的,抛出异常时,copy-in-copy-out 的语义与别名引用的语义不同。
在像这样的奇怪情况下,它们也有所不同:
在别名中,x、q和r都是相同的存储位置,因此这将打印 20。在复制输入复制输出引用中,这将打印 10,并且最终值x 将取决于复制输出是从左到右还是从右到左。
最后,如果我没记错的话,在表达式树的实现中存在罕见且奇怪的场景,我们实际上在 ref 参数上实现了 copy-in-copy-out 语义。我应该查看该代码,看看我是否能记住这些场景到底是什么。
Andrew is correct; I will merely add a couple of extra details.
First off, the correct way to think of out/ref parameters is that they are aliases for variables. That is, when you have a method M(ref int q) and call it M(ref x), q and x are two different names for exactly the same variable. A variable is a storage location; you store something in q, you're storing it in x too, because they are two different names for the same location.
Second, the alternative you're describing is called "copy in / copy out" referencing. In this scheme there are two storage locations and the contents of one are copied in upon the function call beginning, and copied back out when its done. As you note, the semantics of copy-in-copy-out are different than the semantics of alias references when exceptions are thrown.
They are also different in bizarre situations like this:
In aliasing, x, q and r are all the same storage location, so this prints 20. In copy-in-copy-out referencing, this would print 10, and the final value of x would depend on whether the copy-out went left to right or right to left.
Finally, if I recall correctly, there are rare and bizarre scenarios in the implementation of expression trees where we actually implement copy-in-copy-out semantics on ref parameters. I should review that code and see if I can remember what exactly those scenarios are.