When should I use readonly / omit the set accessor / private accessor on a property RRS feed

  • Question

  • Hello

    When should I use readonly property / omit the set accessor / private accessor on a property

    Please explain



    Tuesday, October 2, 2018 5:06 AM

All replies

  • Use readonly when you want to set the value in the constructor, and the value can't change after that. So each instance of the class can have a different value for the property, but that value can't change once set (unlike a const, where the value is the same for all instances).

    Omit the set accessor when the instance of the class might itself change the value of the property, but other classes can not change it (although other classes can read it).

    Use the private accessor when nothing outside the class can change or read the property.

    Tuesday, October 2, 2018 5:25 AM
  • As an extension to Ante's description. It is important to distinguish between "full" properties and auto properties.

    A) A full property means you provide the getter and setter implementations. If you leave off one or the other then nobody can get/set that property. That includes the type itself. This is most useful for calculated properties that have no backing field.

    B) Making a getter/setter private means only the owning type can get/set it. This is useful for property that can only be set during construction. 

    C) Auto properties are when you just specify the "public" accessor(s) and let the compiler generate the backing field and private accessor (if any).

    D) Auto properties may specify mixed accessibility. So a private setter would mean the property could only be set by the owning type.

    E) Auto properties may leave off the setter altogether. In this case the compiler generates a private setter but it also marks the field as readonly so it can only be set during the constructor (or via the field initializer).

    F) You can use a lambda with an auto property as well. In this case you're back to a calculated property and nobody can set the value.

    class MyClass
        public MyClass ()
            Width2 = 10;
        //A: Full property, calculated value, nobody can set it
        public int Area
            get { return 100; }
        //B: Full property, private setter, only owning type can set it
        public int Id
            get { return _id; }
            private set { _id = value; }
        private int _id;
        //C: Auto property, anybody can get/set it
        public int Length { get; set; }
        //D: Auto property, only owning type can set it
        public int Width { get; private set; }
        //E: Auto property, setter is private and field is marked readonly
        public int Width2 { get; }
        //F: Auto property, same as Area, nobody can set it
        public int Area2 => 100;
        private void Foo ()
            //A: ERROR Cannot set full properties with no setters
            Area = 10;
            //B: Can set full properties with private setters
            Id = 1;
            //C: Auto property, anybody can do anything
            Length = 80;
            //D: Auto property, only owning type can set it
            Width = 4;
            //E: ERROR Auto property again, implicitly generated setter and field is readonly
            Width2 = 5;
            //F: ERROR Auto property with no setter, nobody can set it
            Area2 = 40;

    Michael Taylor

    Tuesday, October 2, 2018 3:01 PM