C# 自动属性

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

C# Automatic Properties

c#c#-3.0automatic-properties

提问by Gavin

I'm a bit confused on the point of Automatic properties in C# e.g

我对 C# 中的自动属性点有点困惑,例如

public string Forename{ get; set; }

I get that you are saving code by not having to declare a private variable, but what's the point of a property when you are not using any get or set logic? Why not just use

我知道您不必声明私有变量就可以节省代码,但是当您不使用任何 get 或 set 逻辑时,属性的意义何在?为什么不直接使用

public string Forename; 

I'm not sure what the difference between these 2 statements is, I always thought you used properties if you wanted additional get/set logic?

我不确定这两个语句之间的区别是什么,我一直认为如果您想要额外的 get/set 逻辑,您使用了属性?

采纳答案by MatthewMartin

Properties can have code put into them without breaking contract, fields can't have code put into them without changing them to properties (and breaking the interface). Properties can be read only or write only, fields can't. Properties can be data bound, fields can't.

属性可以在不违反合同的情况下放入代码,字段不能在不将它们更改为属性(并破坏接口)的情况下将代码放入其中。属性可以只读或只写,字段不能。属性可以是数据绑定的,字段不能。

回答by Gavin

For one, you can set the property to virtual and implement logic in an inheriting class. You can also implement logic in the same class afterwards and there won't be side-effects on any code relying on the class.

一方面,您可以将属性设置为 virtual 并在继承类中实现逻辑。之后您也可以在同一个类中实现逻辑,并且不会对依赖于该类的任何代码产生副作用。

回答by Gavin

It is meant that you expect to add the logic later.

这意味着您希望稍后添加逻辑。

If you do so and have it as property from the beginning, you will not have to rebuild the dependent code. If you change it from a variable to a property, then you will have to.

如果您这样做并从一开始就将其作为属性,您将不必重新构建依赖代码。如果您将其从变量更改为属性,那么您将不得不这样做。

回答by Gishu

Public data members are evil (in that the object doesn't control modification of it's own state - It becomes a global variable). Breaks encapsulation - a tenet of OOP.

公共数据成员是邪恶的(因为对象不控制对其自身状态的修改——它变成了一个全局变量)。打破封装——OOP 的一个原则。

Automatic properties are there to provide encapsulation and avoid drudgery of writing boiler plate code for simple properties.

自动属性可以提供封装并避免为简单属性编写样板代码的苦差事。

public string ID { get; set;}

You can change automatic properties to non-automatic properties in the future (e.g. you have some validation in a setter for example)... and not break existing clients.

您可以在将来将自动属性更改为非自动属性(例如,您在 setter 中有一些验证)......并且不会破坏现有客户端。

string m_ID;
public string ID
{
   get { return m_ID; }
   set 
   { 
     //validate value conforms to a certain pattern via a regex match
     m_ID = value;
   }
}

You cannot do the same with public data attributes. Changing a data attribute to a property will force existing clients to recompile before they can interact again.

你不能对公共数据属性做同样的事情。将数据属性更改为属性将强制现有客户端重新编译,然后才能再次交互。

回答by Sklivvz

You can write

你可以写

public string Forename{ get; private set; }

to get read-only properties... Still not nearly as versatile as real properties, but it's a compromise that for some works.

获得只读属性......仍然不像真实属性那样通用,但对于某些作品来说这是一种妥协。

回答by Reed Copsey

I'm not sure what the difference between these 2 statements is, I always thought you used properties if you wanted additional get/set logic?

我不确定这两个语句之间的区别是什么,我一直认为如果您想要额外的 get/set 逻辑,您使用了属性?

In the first case, the compiler will automatically add a field for you, and wrap the property. It's basically the equivalent to doing:

在第一种情况下,编译器会自动为您添加一个字段,并包装该属性。这基本上相当于做:

private string forename;
public string Forename
{
    get
    { 
        return this.forename;
    }
    set
    {
        this.forename = value;
    }
}

There are many advantages to using properties over fields. Even if you don't need some of the specific reasons, such as databinding, this helps to future-proof your API.

与字段相比,使用属性有很多优点。即使您不需要某些特定原因(例如数据绑定),这也有助于您的 API 面向未来。

The main problem is that, if you make a field, but in v2 of your application, need a property, you'll break the API. By using an automatic property up front, you have the potential to change your API at any time, with no worry about source or binary compatibility issues.

主要问题是,如果您创建一个字段,但在您的应用程序的 v2 中,需要一个属性,您将破坏 API。通过预先使用自动属性,您可以随时更改 API,而无需担心源代码或二进制兼容性问题。

回答by MattH

When adding auto properties the compiler will add get set logic into the application, this means that if you later add to this logic, and references to your property from external libraries will still work.

添加自动属性时,编译器会将 get set 逻辑添加到应用程序中,这意味着如果您稍后添加到此逻辑,并且从外部库对您的属性的引用仍然有效。

If you migrated from a public variable to a property, this would be a breaking change for other libraries that reference yours - hence, why not start with an auto property? :)

如果你从一个公共变量迁移到一个属性,这对于引用你的其他库来说将是一个重大变化——因此,为什么不从一个自动属性开始呢?:)

回答by user29117

Not all properties need get/set logic. If they do, you use a private variable. For example, in a MV-something pattern, your model would not have much logic. But you can mix and match as needed.

并非所有属性都需要获取/设置逻辑。如果他们这样做,您将使用私有变量。例如,在 MV-something 模式中,您的模型不会有太多逻辑。但是您可以根据需要混合搭配。

If you were to use a field like you suggested in place of a property, you can't for example define an interface to describe your class correctly, since interfaces cannot contain data fields.

如果您要使用您建议的字段代替属性,则不能例如定义接口来正确描述您的类,因为接口不能包含数据字段。

回答by Ravi Ravilla

A property is like a contract, and you can change the implemenation of a property without affecting the clients using your classes and properties. You may not have any logic today, but as business requirements change and if you want to introduce any code, properties are your safest bet. The following 2 links are excellent c# video tutorials. The first one explains the need of properties over just using fields and the second video explains different types of properties. I found them very useful.

属性就像合同,您可以更改属性的实现,而不会影响使用您的类和属性的客户端。您今天可能没有任何逻辑,但随着业务需求的变化,如果您想引入任何代码,属性是您最安全的选择。以下 2 个链接是优秀的 c# 视频教程。第一个解释了仅使用字段需要属性,第二个视频解释了不同类型的属性。我发现它们非常有用。

Need for the Properties in C#

需要 C# 中的属性

Poperties in C#, Read Only, Write Only, Read/Write, Auto Implemented

C# 中的属性,只读,只写,读/写,自动实现