understanding private setters

Viewed 167127

I don't understand the need of having private setters which started with C# 2.

Having a setter method for me is letting the user to set some variables in that class. In doing so, we will not expose the variables directly to the users. Instead we let them do it through this public setter method.

This for me is using "encapsulation". There are some arguments out there which claim that private setters will let you apply encapsulation.

Am I not using encapsulation by using public setter methods? Why do we need private setters?

What is the difference between immutable class and a class with private setters?

13 Answers

Credits to https://www.dotnetperls.com/property.

private setters are same as read-only fields. They can only be set in constructor. If you try to set from outside you get compile time error.

public class MyClass
{
    public MyClass()
    {
        // Set the private property.
        this.Name = "Sample Name from Inside";
    }
     public MyClass(string name)
    {
        // Set the private property.
        this.Name = name;
    }
    string _name;
    public string Name
    {
        get
        {
            return this._name;
        }
        private set
        {
            // Can only be called in this class.
            this._name = value;
        }
    }
}

class Program
{
    static void Main()
    {
        MyClass mc = new MyClass();
        Console.WriteLine(mc.name);

        MyClass mc2 = new MyClass("Sample Name from Outside");
        Console.WriteLine(mc2.name);
    }
}

Please see below screen shot when I tried to set it from outside of the class.

enter image description here

Despite the easy an clean way to write both, readonly, auto and private setters are things totally different.

Readonly properties are simply a fast and clean way to declare constants/statics in code:

   //constants
   public const int Constant = 50;
   public readonly int ReadOnly = 50;
   public int ConstantMethod() 
   {
       return 50;
   }

And for statics:

   public static readonly int Value = 50;
   public static int ValueMethod()
   {
       return 50;
   }

Any of them is the same thing, works the same way, except for readonly, that you can initialize in constructor. Even so, that´s a matter of how you like to code, changes nothing the results.

Remembering that static readonly is for types that don´t have literals, for methods inside classes or structs. Remembering also that new invokes the constructor of a class/struct.

Auto is the placeholder of:

    private int field;
    public int Property 
    {
        get
        {
            return field;
        }
         
        set
        {
            field = value;
        }
     }

     //In the auto property case, the "field" field is supressed, but it´s still 
     //there. 
       public int Property {get; set;}

So, private setters comes in auto properties, in any of this examples above. The keyword to considerate is private.

private, internal, protected and public are access keywords. So, if you declare:

    public int Property { get; private set; }

You just are saying that you want that the "set" part of Property can be changed only for the class itself.

    public int Property { get; protected set; }

Say that the "set" part of Property can be changed only by the class itself or for nested class of the base class. Also:

     private int Property { get; set; }
     public int Property {private get; private set; }

Both are the same. This is because the modifier that goes with the Property itself reflects in both "get" and "set" parts.

Related