是否有可能通过属性和QUOT;出"或QUOT;参考"参数? [英] Is it possible to pass properties as "out" or "ref" parameters?

查看:193
本文介绍了是否有可能通过属性和QUOT;出"或QUOT;参考"参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以传递一个属性作为走出去,或如果没有的话,为什么裁判的参数呢?

例如

 人员P =新的Person();



 公共无效测试(满分p.Name);


解决方案

道歉,简单的答案,但没有,C#语言规范禁止它。

请参阅此<一个href=\"http://stackoverflow.com/questions/340528/c-automatic-properties-why-do-i-have-to-write-get-set/340682#340682\">answer另一个问题看,当你尝试会发生什么。
它还说,为什么你不应该使该属性只是一个公共领域来绕过限制。

希望这有助于

编辑:你问为什么

您传递给退出 REF 参数你实际上传递地址(或位置可变可变的存储器)。
在函数内部编译器知道其中变量确实是,并得到与该地址写入值。

一个属性看起来像一个值,击打它实际上是一对函数,每一个不同的签名。所以要通过一个属性,你实际上需要传递两个函数指针,一个用于获取,一个用于集。

这就是一个完全不同的东西传递给函数不是一个变量的地址

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

更新结果
为什么不C#只是看在这之后我们呢?

我不是埃里克利珀,但我会在走,为什么

你应该会调用该函数的签名呢?结果
比方说,你要拨打无效MyFn(REF INT I)应该是继续下去的话,还是应该将其更改为说,我们也允许属性?如果它改变一些语法像无效MyFn(prop_ref int i)以那么这是相当无用的,可以不通过性能库函数或第三方code,它没有与特殊prop_ref改性写入。无论如何,我认为你是在暗示它不应该是不同的。

现在让我们说 MyFn 通行证 I 来一个COM功能,或WinAPI的电话,传递的地址 I (即外.NET,由参)。如果它是一个财产,你如何让 I 的地址?可能有物业在没有实际INT获得的地址。你做的VB.Net做什么?

当一个属性作为ByRef参数的方法传递的Vb.Net编译斑点。在这一点上,它声明一个变量,复制属性变量,通过该变量按地址再经过方法被调用,复制变回财产。即。

  MYFUNC(myObject.IntProperty)

变为

 昏暗TEMP_I作为整数= myObject.IntProperty
MYFUNC(TEMP_I)
myObject.IntProperty = TEMP_I

任何财产的副作用不发生,直到 MYFUNC 的回报,这可能会导致各种问题而导致的非常的微妙的错误。

在我的愚见的Vb.Net解决这个问题也坏了,所以我不会接受,作为一个答案。

您如何看待C#编译器应该处理呢?

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

e.g.

Person p = new Person();

. . .

public void Test(out p.Name);

解决方案

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.

Hope this helps

EDIT: You ask Why?

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

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

Update
Why doesn't C# just look after this for us?

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

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.

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?

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)

becomes

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

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

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.

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

这篇关于是否有可能通过属性和QUOT;出&QUOT;或QUOT;参考&QUOT;参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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