为什么在 C# 中使用简单属性而不是字段?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2374416/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:52:26  来源:igfitidea点击:

Why use simple properties instead of fields in C#?

c#

提问by Robert Davis

Possible Duplicates:
Should I use public properties and private fields or public fields for data?
Difference between Automatic Properties and public field in C# 3.0

可能的重复:
我应该对数据使用公共属性和私有字段还是公共字段?
C# 3.0 中自动属性和公共字段的区别

People seem to dogmatically insist on the use of public properties over fields but why is it so ultra-important in the case of simple properties?

人们似乎教条地坚持在字段上使用公共属性,但为什么在简单属性的情况下它如此重要?

How is

怎么

public int Foo { get; set; }

so incredibly different than

非常不同

public int Foo;

?

?

Off the top of my head I can think of few practical differences between the two:

在我的脑海中,我可以想到两者之间的一些实际差异:

  • Accessing the member using reflection (rare, and most decent reflective algorithms will account for the difference)
  • The second entry allows you to use the field as a valid parameter for ref and out parameters, which would seem to be an advantage to using the field version
  • Fields don't work in Remoting (probably, I've never used remoting but I imagine they wouldn't)?
  • 使用反射访问成员(很少见,大多数体面的反射算法将解释差异)
  • 第二个条目允许您将字段用作 ref 和 out 参数的有效参数,这似乎是使用字段版本的优势
  • 字段在远程处理中不起作用(可能,我从未使用过远程处理,但我想它们不会)?

Other than these fairly rare cases, changing Foo to be a computed property later results in 0 lines of code changed.

除了这些相当罕见的情况外,稍后将 Foo 更改为计算属性会导致更改 0 行代码。

采纳答案by Reed Copsey

Using properties has a couple of distinct advantages:

使用属性有几个明显的优点:

  • It allows for versioning if later you need extra logic. Adding logic to the getter or setter won't break existing code.
  • It allows data binding to work properly (most data binding frameworks don't work with fields).
  • 如果以后需要额外的逻辑,它允许版本控制。向 getter 或 setter 添加逻辑不会破坏现有代码。
  • 它允许数据绑定正常工作(大多数数据绑定框架不适用于字段)。

In addition, there are almost no disadvantages. Simple, automatic properties like this get inlined by the JIT compiler, so there is no reason not to use them.

此外,几乎没有缺点。像这样的简单、自动的属性会被 JIT 编译器内联,所以没有理由不使用它们。

Also, you mentioned:

另外,你提到:

Other than these fairly rare cases, changing Foo to be a computed property later results in 0 lines of code changed.

除了这些相当罕见的情况外,稍后将 Foo 更改为计算属性会导致更改 0 行代码。

This doesn't require your code to be changed, but it does force you to recompile all of your code. Changing from a field to a property is a breaking API change which will require any assembly which references your assembly to be recompiled. By making it an automatic property, you can just ship a new binary, and maintain API compatibility. This is the "versioning" advantage I mentioned above...

这不需要更改您的代码,但会强制您重新编译所有代码。从字段更改为属性是一个破坏性的 API 更改,这将需要重新编译引用您的程序集的任何程序集。通过使其成为自动属性,您可以只发布一个新的二进制文件,并保持 API 兼容性。这就是我上面提到的“版本控制”优势......

回答by David Hogue

Mostly because of convention.

主要是因为约定俗成。

The one solid argument for it is that if you later need to change from a field to a property, then all assemblies that reference yours will need to be recompiled.

一个可靠的论点是,如果您以后需要从字段更改为属性,则需要重新编译所有引用您的程序集的程序集。

Reflection does come into it once in a while, but very rarely. Some serialization types are based off of properties.

反思确实偶尔会出现,但很少。一些序列化类型基于属性。

回答by John Buchanan

One good reason is that you can vary the get/set accessibility.

一个很好的理由是您可以改变 get/set 的可访问性。

public int Foo {get; protected set;}

回答by JonoW

You can make properties virtual and override their implementation in derived classes. This is an important factor in many libraries that wrap your objects in generated proxy classes, e.g. the way NHibernate does to implement lazy loading. This isn't possible on fields.

您可以将属性设为虚拟并在派生类中覆盖它们的实现。这是许多将对象包装在生成的代理类中的库中的一个重要因素,例如 NHibernate 实现延迟加载的方式。这在字段上是不可能的。

回答by Eric Lippert

A property is a language element which logically represents a property of the thing being modeled by the class. The class Car models a car; colour is a property of cars; therefore, Color is a property of Car.

属性是一种语言元素,它在逻辑上表示由类建模的事物的属性。类 Car 建模汽车;颜色是汽车的属性;因此,颜色是 Car 的属性。

A field is an language element which represents an implementation detail of the class. Your car does not have a "colour field", so your program's representation of a car should not expose a field called Color. It might contain a private implementation detail whereby the property Color is implemented by a field, but that's a private implementation detail, not a publically accessible part of the model.

字段是表示类的实现细节的语言元素。您的汽车没有“颜色字段”,因此您的汽车程序表示不应公开名为颜色的字段。它可能包含一个私有实现细节,其中属性 Color 由一个字段实现,但这是一个私有实现细节,而不是模型的可公开访问的部分。