C#:可以“输出”函数中的参数是对象属性/变量吗?
C#:函数中的“输出”参数可以是对象属性/变量吗?
例如:
我可以按如下方式调用函数吗:
someFunction(x, y, out myObject.MyProperty1)
C#: can 'out' parameters in functions be object properties/variables?
eg:
can I call a function as follows:
someFunction(x, y, out myObject.MyProperty1)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
属性是[一对 get 和 set] 方法,具有用于调用它们的可爱语法,使它们看起来像字段。
输出参数可以是对字段或基于堆栈的“字段”(即局部变量)的引用。
语言通常无法弥补这一差距(并且 CLR 和 IL 模型也绝对不能直接解决)。 (+1 @Heinzi 的 VB 欺骗参考)
Properties are [a pair of get and set] methods with a cute syntax for invoking them that makes them look like fields.
Out parameters can be references to fields or stack based 'fields' (i.e., locals).
The bridging of this gap is generally not addressed by languages (and definitely not directly by the CLR and IL model). (+1 on @Heinzi for the VB trickery references)
您无法使用 C# 执行此操作。
您可以使用 VB.Net,但我认为这是一个坏主意。下面的代码和输出显示了它是如何做到这一点的,并显示了为什么我认为这是一个坏主意,以至于我希望 VB.net 也不允许这样做
现在从子主程序中调用它,就像这样
这个输出
您可以看到,传递
ByRef
后,PublicInt
成员会立即更新到 SampleClass 上,将PrivateInt
作为属性公开的内容仅在IterateAndUpdate
方法结束后更新。因此,您从完全相同的调用约定中获得明显不同的行为,这取决于您如何实现传递的项目(从对
IterateAndUpdate
的调用来看,这一点并不明显。隐藏错误或进行微小更改改变预期行为的潜力足以让我希望这个“功能”不存在。
在我看来,这不能正常工作,因此它应该是
a) 已修复,这需要编译器团队付出相当大的努力,并且可能会引入重大更改
或
b) 根本不起作用
You can't do this with C#.
You can with VB.Net, but I consider this a bad idea. The following code and output show how it does it and shows why I think it's a bad idea, to the point where I wish VB.net also didn't allow this
Now call this from a sub main, like so
This outputs
You can see that the
PublicInt
member when passedByRef
is updated immediately on sampleClass, put thePrivateInt
exposed as a property is only updated after theIterateAndUpdate
method ends.Therefore, you get markedly different behaviour from exactly the same calling convention, and it depends on how you've implemented the item passed (which isn't at all obvious looking at the call to
IterateAndUpdate
.The potential to hide bugs, or have small changes change expected behaviour is enough for me to wish this "feature" didn't exist.
To my mind this does not work correctly, therefore it should either be
a) Fixed, which would take considerable effort for the compiler team, and possibly introduce breaking changes
or
b) Not work at all
不可以,您不能在 C# 中使用属性作为
ref
或out
参数,因为 CLR 不支持这一点。传递实例变量应该可以正常工作。附带说明一下,VB.NET 允许传递属性并使用称为“copy back ByRef”的技术,如 此 MSDN 博客条目。
No, you cannot use a property as a
ref
orout
parameter in C#, since the CLR does not support this. Passing an instance variable should work fine.As a side note, VB.NET allows passing properties and uses a technique called "copy back ByRef", as explained in this MSDN blog entry.
传递给输出参数的参数不能是属性。如果您尝试编译代码,您将收到编译错误。
原因是属性实际上不是字段(它可能有支持字段,但也可能没有)。它有两个方法,分别称为
get_Foo
和set_Foo
。请参阅 c# 属性和 ref 参数,为什么没有糖?更多解释为什么这不起作用。给出编译错误的示例代码:
The argument passed to an out parameter cannot be a property. If you try to compile your code you will get a compile error.
The reason is that a property is not actually field (it might have a backing field, but it might not). It is two methods called
get_Foo
andset_Foo
. See c# property and ref parameter, why no sugar? for more explanation of why this doesn't work.Example code that gives the compile error:
您可以将
out
参数与字段(或已经说过的本地变量)一起使用。You can use the
out
parameter with fields (or locals as said already).