C# 字段与属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8773181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# field vs. property
提问by Baruch
Possible Duplicate:
Difference between Property and Field in C#
可能的重复:
C# 中属性和字段的区别
I thought that basic properties ({ get; set; }) where the same as public fields, with only the advantage of being able to change them without breaking binary compatibility. Following the answer I got here https://stackoverflow.com/a/8735303/331785, I found out there is also a disadvantage to properties. They cannot be accessed by reference if they are of a value type. Why is this, and what other differences are there?
我认为基本属性 ( { get; set; }) 与公共字段相同,唯一的优点是能够在不破坏二进制兼容性的情况下更改它们。按照我在这里得到的答案https://stackoverflow.com/a/8735303/331785,我发现属性也有缺点。如果它们是值类型,则不能通过引用访问它们。这是为什么,还有什么其他区别?
采纳答案by vcsjones
I found out there is also a disadvantage to properties. They cannot be accessed by reference if they are of a value type. Why is this
我发现属性也有缺点。如果它们是值类型,则不能通过引用访问它们。为什么是这样
Because under the covers, a property is just a method. If you look at the IL, you'll see methods like get_PropertyNameand set_PropertyName. The problem with that is in order to support working with references, you would need to be able to return a reference for a method.
因为在幕后,属性只是一种方法。如果您查看 IL,您会看到类似get_PropertyName和的方法set_PropertyName。问题是为了支持使用引用,您需要能够返回一个方法的引用。
public ref T MyProperty
{
get
{
return ref _underlyingField;
}
}
Update: Starting in C# 7.0, this is possible using the syntax describe above.
更新:从 C# 7.0 开始,这可以使用上述语法实现。
Remainder of previous answer:
上一个答案的剩余部分:
This of course, is something entirely possible in the CLR; but not exposed by the C# language.
当然,这在 CLR 中是完全可能的;但没有被 C# 语言公开。
Though it is possible, the CLR needs some tweaks to keep it as verifiable. The syntax for the property would have to support it to.
尽管这是可能的,但 CLR 需要进行一些调整以使其可验证。属性的语法必须支持它。
However, is any of that useful? As you stated, a field can do it. If you need it; use a field. Supporting it would take a lot of work. There are probably a very few cases where it is appropriate; and would create many cases where just using a field might have been better in the first place.
但是,这些有用吗?正如你所说,一个领域可以做到。如果你需要它;使用一个字段。支持它需要做很多工作。可能只有极少数情况是合适的;并且会产生许多情况,在这些情况下,首先只使用一个字段可能会更好。
回答by Robert Rouhani
Properties are just sugar-coating syntax for a getX()and setX()method. It looks and acts like a field, but it's really just two methods. The reason why the auto-property was added was to avoid the repetition of having to create a field and creating a standard getter and setter for the property, and to make it much simpler to allow changing the implementation without changing the interface.
属性只是 agetX()和setX()方法的糖衣语法。它看起来和行为就像一个字段,但它实际上只是两种方法。添加 auto-property 的原因是为了避免重复必须创建字段并为属性创建标准 getter 和 setter,并使允许在不更改接口的情况下更改实现变得更加简单。
The reason they can't be accessed by reference if they are a value type is because value types are generally on the stack and because you're just calling a method. The getter in the property has to be called and the returned value has to be pushed on the stack before it can be referenced.
如果它们是值类型,则不能通过引用访问它们的原因是因为值类型通常在堆栈上,并且因为您只是在调用一个方法。必须调用属性中的 getter,并且必须先将返回值压入堆栈,然后才能引用它。

