Public Fields versus Automatic Properties

前端 未结 12 2085
梦谈多话
梦谈多话 2020-11-21 23:34

We\'re often told we should protect encapsulation by making getter and setter methods (properties in C#) for class fields, instead of exposing the fields to the outside worl

相关标签:
12条回答
  • 2020-11-22 00:14

    Ignoring the API issues, the thing I find most valuable about using a property is debugging.

    The CLR debugger does not support data break points (most native debuggers do). Hence it's not possible to set a break point on the read or write of a particular field on a class. This is very limiting in certain debugging scenarios.

    Because properties are implemented as very thin methods, it is possible to set breakpoints on the read and write of their values. This gives them a big leg up over fields.

    0 讨论(0)
  • 2020-11-22 00:14

    Another advantage of auto-implemented properties over public fields is that you can make set accessors private or protected, providing the class of objects where it was defined better control than that of public fields.

    0 讨论(0)
  • 2020-11-22 00:20

    A huge difference that is often overlooked and is not mentioned in any other answer: overriding. You can declare properties virtual and override them whereas you cannot do the same for public member fields.

    0 讨论(0)
  • 2020-11-22 00:24

    My pov after did some researches

    1. Validation.
    2. Allow overriding the accessor to change the behaviour of a property.
    3. Debugging purpose. We'll be able to know when and what the property change by setting a breakpoint in the accessor.
    4. We can have a field set-only. For instance, public set() and private get(). This is not possible with the public field.

    It really gives us more possibility and extensibility.

    0 讨论(0)
  • 2020-11-22 00:28

    Just because no one mentioned it: You can't define fields on Interfaces. So, if you have to implement a specific interface which defines properties, auto-properties sometimes are a really nice feature.

    0 讨论(0)
  • 2020-11-22 00:28

    One thing you can do with Fields but not with Properties (or didn't used to be able to ... I'll come to that in a moment) is that Fields can be designated as readonly whereas Properties cannot. So Fields give you a clear way of indicating your intention that a variable is there to be set (from within the constructor) at object-instantiation time only and should not be changed thereafter. Yes, you can set a Property to have a private setter, but that just says "this is not to be changed from outside the class", which is not the same as "this is not to be changed after instantiation" - you can still change it post-instantiation from within the class. And yes you can set the backing field of your property to be readonly, but that moves post-instantiation attempts to change it to being run-time errors rather than compile-time errors. So readonly Fields did something useful which Properties cannot.

    However, that changes with C# 9, whereby we get this helpful syntax for Properties:

    public string Height { get; init; }
    

    which says "this can get used from outside of the class but it may only be set when the object is initialized", whereupon the readonly advantage of Fields disappears.

    0 讨论(0)
提交回复
热议问题