是否可以将属性作为“out"传递?或“参考"参数? [英] Is it possible to pass properties as "out" or "ref" parameters?

查看:31
本文介绍了是否可以将属性作为“out"传递?或“参考"参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果不能,我可以将属性作为out"或ref"参数传递,那为什么不呢?

Can I pass a property as an "out" or "ref" parameter if not then why not?

例如

Person p = new Person(); 

...

public void Test(out p.Name);

推荐答案

对于简短的回答深表歉意,但不,C# 语言规范不允许这样做.

Apologies for the short answer, but no, the C# language specification disallows it.

请参阅此答案 到另一个问题,看看当你尝试时会发生什么.它还说明了为什么你不应该将财产仅仅作为一个公共领域来绕过限制.

See this answer to another question to see what happens when you try. It also says why you shouldn't make the property just be a public field to get around the restriction.

希望能帮到你

你问为什么?

您将变量传递给 outref 参数,您实际上是在传递变量的地址(或内存中的位置).在函数内部,编译器知道变量的实际位置,并获取值并将其写入该地址.

You pass a variable to an out or ref parameter you're actually passing the address (or location in memory) of the variable. Inside the function the compiler knows where the variable really is, and gets and writes values to that address.

属性看起来像一个值,但实际上它是一对函数,每个函数都有不同的签名.所以要传递一个属性,你实际上需要传递两个函数指针,一个用于获取,一个用于集合.

A property looks like a value, buts it's actually a pair of functions, each with a different signature. So to pass a property, you'd actually need to pass two function pointers, one for the get, and one for the set.

传递给函数的东西与传递给变量的地址完全不同

Thats a completely different thing to pass to a function than the address of a variable

即一个变量地址 v 的两个函数指针.

i.e. one variable address v's two function pointers.

更新
为什么 C# 不为我们处理这个问题?

我不是埃里克·利珀特,但我会想知道为什么

I'm no Eric Lippert, but I'll have a go at why

你正在调用的函数的签名应该是什么?
假设您想调用 void MyFn(ref int i) 应该保持这种方式,还是应该更改为我们也允许属性?如果它更改为诸如 void MyFn(prop_ref int i) 之类的语法,那么这是相当无用的,您无法将属性传递给未使用特殊 prop_ref 修饰符编写的库函数或第 3 方代码.无论如何,我认为你建议它不应该有所不同.

What should the signature of the function you're calling be?
Lets say you want to call void MyFn(ref int i) should that remain that way, or should it change to say we also allow properties? If it changes to some syntax like void MyFn(prop_ref int i) then this is fairly useless, you can't pass properties to library functions or 3rd party code that wasn't written with the special prop_ref modifier. Anyway I think you're suggesting it shouldn't be different.

现在让我们说 MyFni 传递给 COM 函数或 WinAPI 调用,传递 i 的地址(即在 .net 之外,通过参考).如果是属性,如何获取i的地址?属性下可能没有实际的 int 来获取其地址.你做 VB.Net 做的事吗?

Now lets say MyFn passes i to a COM function, or WinAPI call, passing the address of i (i.e. outside .net, by ref). If it's a property, how do you get the address of i? There may be no actual int under the property to get the address of. Do you do what VB.Net does?

当属性作为 ByRef 参数传递给方法时,Vb.Net 编译器会发现.此时它声明一个变量,将属性复制到变量,通过引用传递变量,然后在调用方法后,将变量复制回属性.即

The Vb.Net compiler spots when a property is passed as a ByRef argument to a method. At that point it declares a variable, copies the property to the variable, passes the variable byref and then after the method is called, copies the variable back into the property. i.e.

MyFunc(myObject.IntProperty)

变成

Dim temp_i As Integer = myObject.IntProperty
MyFunc(temp_i)
myObject.IntProperty = temp_i

MyFunc 返回之前不会发生任何属性副作用,这可能会导致各种问题并导致非常 细微的错误.

Any property side effects don't happen until MyFunc returns, which can cause all sorts of problems and lead to very subtle bugs.

在我看来,这个问题的 Vb.Net 解决方案也被破坏了,所以我不会接受它作为答案.

In my humble opinion the Vb.Net solution to this problem is also broken, so I'm not going to accept that as an answer.

您认为 C# 编译器应该如何处理这个问题?

How do you think the C# compiler should handle this?

这篇关于是否可以将属性作为“out"传递?或“参考"参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆