简体   繁体   English

了解自动实现的属性

[英]Learning about Auto-Implemented Properties

I have the simple class using auto-implemented properies: 我有使用自动实现的属性的简单类:

Public Class foo
{
    public foo() { }  

    public string BarName {get; set;}
}

I obviously use the variable BarName throughout my class and now need to add logic when the property value is set (it must be all upper case, go figure). 我显然在我的类中使用变量BarName,现在需要在设置属性值时添加逻辑(它必须全部为大写,如图)。 Does this mean that I need to now create a private variable for BarName , eg _BarName, and change the current BarName variable used throughout my class to _BarName? 这是否意味着我现在需要为BarName创建一个私有变量,例如_BarName,并将我的类中使用的当前BarName变量更改为_BarName?

Public Class foo
{
    public foo() {}  

    private string _BarName = "";
    public string BarName 
    { 
        get {return _BarName;}
        set {_BarName = Value.ToString().ToUpper();}
    }
}

I am trying to make sure I understand the implications of using auto-implemented properties, and what it will entail down the road when/if I need to change something. 我正在努力确保我理解使用自动实现的属性的含义,以及当我/如果我需要更改某些内容时它会带来什么。 I am assuming that the refactoring, as shown above, is not a breaking change because the property is basically staying the same; 我假设重构,如上所示,不是一个突破性的变化,因为该属性基本上保持不变; it just took a little work inside the class to keep it that way and add the needed logic. 它只是在课堂上做了一些工作,以保持这种方式并添加所需的逻辑。

Another example, which may be more meaningful is that I need to call some method when a setter or getter is used; 另一个可能更有意义的例子是我需要在使用setter或getter时调用某个方法; more then changing the value. 更多然后改变价值。

This seems a fair trade off the the lines and lines of code to setup properties. 这似乎是设置属性的代码行和行的公平交易。

Does this mean that I need to now create a private variable for BarName 这是否意味着我现在需要为BarName创建一个私有变量

Yes

and change the current BarName variable used throughout my class 并更改我班级中使用的当前BarName变量

Do not change the rest of the code in your class to use the new private variable you create. 不要更改类中的其余代码以使用您创建的新私有变量。 BarName , as a property, is intended to hide the private variable (among other things), for the purpose of avoiding the sweeping changes you contemplate to the rest of your code. BarName作为一个属性,旨在隐藏私有变量(以及其他内容),以避免您考虑对代码的其余部分进行全面更改。

I am assuming that the refactoring, as shown above, is not a breaking change because the property is basically staying the same; 我假设重构,如上所示,不是一个突破性的变化,因为该属性基本上保持不变; it just took a little work to keep it that way and add the needed logic. 只需花一点时间就可以保持这种状态并添加所需的逻辑。

Correct. 正确。

You don't need to change anything. 你不需要改变任何东西。 Auto-implemented properties are just syntactic sugar. 自动实现的属性只是语法糖。 The compiler is generating the private variable and get/set logic for you, behind the scenes. 编译器在后台为您生成私有变量和get / set逻辑。 If you add your own getter/setter logic the compiler will use your code instead of its auto-generated code, but as far as the users of that property are concerned, nothing has changed; 如果添加自己的getter / setter逻辑,编译器将使用您的代码而不是其自动生成的代码,但就该属性的用户而言,没有任何改变; any code referencing your property will continue to work. 任何引用您的财产的代码都将继续有效。

When using automatic properties you don't get direct access to the underlying "backing" variable and you don't get access to the actual logic that gets implemented in the property getter and setter. 使用自动属性时,您无法直接访问基础“支持”变量,也无法访问在属性getter和setter中实现的实际逻辑。 You only have access to the property (hence using BarName throughout your code). 您只能访问该属性(因此在整个代码中使用BarName)。

If you now need to implement specific logic in the setter, you can no longer use automatic properties and need to implement the property in the "old fashioned" way. 如果您现在需要在setter中实现特定逻辑,则不能再使用自动属性,而是需要以“老式”方式实现该属性。 In this case, you would need to implement your own private backing variable (the preferred method, at least for me, is to name the private backing variable the same name as the property, but with an initial lowercase (in this case, the backing variable would be named barName). You would then implement the appropriate logic in the getter/setter. 在这种情况下,您需要实现自己的私有支持变量(首选方法,至少对我而言,是将私有支持变量命名为与属性相同的名称,但使用初始小写(在本例中为支持)变量将命名为barName)。然后,您将在getter / setter中实现适当的逻辑。

In your example, you are correct that it is not a breaking change. 在您的示例中,您是正确的,它不是一个重大变化。 This type of refactoring (moving from automatic properties to "normal" properties should never be a breaking change as you aren't changing the public interface (the name or accessibility of the public property). 这种类型的重构(从自动属性移动到“普通”属性不应该是一个重大变化,因为您不更改公共接口(公共属性的名称或可访问性)。

Don't use automatic properties if you know that you are going to validate that object. 如果您知道要验证该对象,请不要使用自动属性。 These objects can be domain objects etc. Like if you have a Customer class then use private variables because you might need to validate the name, birthdate etc. But if you are using a Rss class then it will be okay to just use the automatic properties since there is no validation being perform and the class is just used to hold some data. 这些对象可以是域对象等。如果你有一个Customer类,那么使用私有变量,因为你可能需要验证名称,birthdate等。但是如果你使用的是Rss类,那么只需使用自动属性就可以了。因为没有执行验证,并且该类仅用于保存一些数据。

You are correct about the refactoring and it really shouldn't break anything. 你对重构是正确的,它真的不应该破坏任何东西。

Whether or not you actually need to go through the references within the class to the property name and change those to refer to the private field would depend on whether the internal code needed to access the underlying representation of the data rather than how it was presented to consumers of the class. 您是否确实需要通过类中的引用来查看属性名称并更改它们以引用私有字段,这取决于访问数据的基础表示所需的内部代码是否需要访问数据的基础表示班上的消费者。 In most cases you could leave well enough alone. 在大多数情况下,你可以独自留下足够的空间

In your simple example it would be wise to leave well enough alone and ensure that no code internal to the class could subvert the conversion/formatting being performed in the setter. 在您的简单示例中,最好留下足够的单独并确保类内部的代码不会破坏在setter中执行的转换/格式化。

If on the other hand the getter was doing some magic to change the internal representation of the field into the way consumers needed to view the data then perhaps (in some cases) the internal code within the class would need to access the field. 另一方面,如果getter正在做一些魔术,将字段的内部表示更改为消费者查看数据所需的方式,那么可能(在某些情况下)类中的内部代码需要访问该字段。

You would need to look at each occurrence of the access to the auto-property in the class and decide whether it should be touching the field or using the property. 您需要查看类中每次出现对auto-property的访问,并确定它是应该触摸该字段还是使用该属性。

Automatic properties are just syntactic sugar, the compiler in fact creates the private member for it, but since it's generated at compile time, you cannot access it. 自动属性只是语法糖,编译器实际上为它创建了私有成员,但由于它是在编译时生成的,因此无法访问它。

And later on, if you want to implement getters and setters for the property, only then you create a explicit private member for it and add the logic. 稍后,如果要为属性实现getter和setter,则只需为其创建一个显式私有成员并添加逻辑。

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

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