简体   繁体   English

自动实现的属性c#

[英]Auto-Implemented Properties c#

  1. could someone explain me what's the idea behind using Auto-Implemented Properties c#? 有人可以解释一下使用Auto-Implemented Properties c#背后的想法吗?

     public class Customer { public int ID { get; set; } public string Name { get; set; } } 

    I get the motivation to use properties for private field, so we can determine how one can access a private field. 我有动力使用私有​​字段的属性,因此我们可以确定如何访问私有字段。 But here - it's just like defining the field to be public from the first place. 但在这里 - 就像从一开始就将这个领域定义为公开一样。 no? 没有?

  2. Is there a difference between defining a field to be "public const" or define it to have a get-only property ? 将字段定义为“public const”或将其定义为具有get-only属性之间是否存在差异?

A public automatic property is not the same as a public field, they are not binary compatible. 公共自动属性与公共字段不同,它们不是二进制兼容的。 If you implement a public field and later on want to add some logic, you will have to change it into a property and thereby introduce a breaking change (because of the binary incompatibility). 如果您实现了一个公共字段,并且稍后想要添加一些逻辑,则必须将其更改为属性,从而引入重大更改(因为二进制不兼容)。 This is the reason why many conventions state that you should never expose public fields but rather use properties. 这就是为什么许多约定规定您不应该公开公共字段而是使用属性的原因。

So, automatic properties are just a convenient starting point for any simple non-private class value member, allowing one to add logic later on while keeping binary compatibility. 因此,自动属性只是任何简单的非私有类值成员的一个方便的起点,允许稍后添加逻辑,同时保持二进制兼容性。

属性可以是数据绑定,而字段则不能。

Automatically implemented properties are essentially syntactic sugar. 自动实现的属性基本上是语法糖。 Once compiled, the backing store exists. 编译后,后备存储存在。 It just isn't available from the source code. 它只是源代码中没有。

As others have stated, properties and fields are not equivalent. 正如其他人所说,属性和字段不相同。 Fields and properties aren't compatible so changing between them is a breaking change. 字段和属性不兼容,因此在它们之间进行更改是一个重大变化。 In addition, you cannot use data binding with fields. 此外,您不能使用数据绑定字段。

Final point. 最后一点。 Though in your case there's little functional difference between the example and a public field, you can change the visibility of one of the accessors. 虽然在您的情况下,示例和公共字段之间的功能差异很小,但您可以更改其中一个访问者的可见性。 So, to create a read-only property using an automatic property, you may do something like: 因此,要使用自动属性创建只读属性,您可以执行以下操作:

public int ID { get; private set; }

In this case, the get accessor is public, as per the entire signature, but the set accessor is private. 在这种情况下,get访问器是公共的,根据整个签名,但set访问器是私有的。

I will let MSDN do the talking here.... 我会让MSDN在这里说话....

"In C# 3.0 and later, auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors. They also enable client code to create objects. When you declare a property as shown in the following example (see MSDN article for example), the compiler creates a private, anonymous backing field that can only be accessed through the property's get and set accessors" “在C#3.0及更高版本中,当属性访问器中不需要额外的逻辑时,自动实现的属性使属性声明更简洁。它们还使客户端代码能够创建对象。当您声明属性时,如下例所示 (请参阅例如MSDN文章), 编译器创建一个私有的匿名支持字段,只能通过属性的get和set访问器访问“

Probably the most advantageous difference is you can do pre/post validation, raise PropertyChanged events etc 可能最有利的区别是你可以做前/后验证,提出PropertyChanged事件等

Is there a difference between defining a field to be "public const" or define it to have a get-only property? 将字段定义为“public const”或将其定义为具有get-only属性之间是否存在差异?

Yes, a get-only field must have a private field declaration. 是的,get-only字段必须具有私有字段声明。 This field can be changed by the class internally, marking a field as const means it cannot be modified. 该字段可以在内部由类更改,将字段标记为const意味着它不能被修改。

2: a public const has to be defined at compiletime, you cannot use reference objects for that. 2:必须在编译时定义公共const,不能使用引用对象。 Only classes that inherit from System.ValueType (string, int, double, ...) 只有从System.ValueType继承的类(string,int,double,...)

A const is also static whereas a property with only a getter is not (every class has it's own instance.) const也是静态的,而只有getter的属性不是(每个类都有自己的实例。)

Regarding 1: Using auto-implemented properties is recommended, because you can implement additional logic later on without breaking changes. 关于1:建议使用自动实现的属性,因为您可以在以后实现其他逻辑而不会中断更改。 An example for this can be found at Learning about Auto-Implemented Properties 可以在学习自动实现的属性中找到此示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM