C# init equals private set?

Asked

Viewed 93 times

8

I was reading about the new implementations on C# 9 and came across the init, init-only-setters:

Starting with C# 9.0, you can create init accessors Instead of set accessors for properties and indexers.

In free translation:

From C# 9.0, you can create "advisors" init instead of define advisors set for properties and indexers.

Viewing the code example below:

public class Pessoa
{ 
  public string Nome { get; init; } 
  public string Idade { get; set; } 
} 

You can instantiate the class like this, "in the constructor":

var pessoa = new Pessoa
{
    Nome = "Fulano",
    Idade = 20
};

The estate Idade can be changed after the instance, but not the property Nome:

pessoa.Nome = "Outro";  // isso dá erro

That’s not the same as declaring ownership Nome this way?

public string Nome { get; private set; }

If so, is there any advantage in using the init in place of private set?

1 answer

9


The behaviors of the two access modifiers are quite different. The fact that it is not possible to change the value of the property outside the class is the only similarity.

In fact, the properties init-only have more similarity to properties getter-only. In this case the difference is that the properties init-only can be initialized in the construction of an object (using the object initialization syntax) and the properties getter-only can only be initialized in a constructor itself.

Back to the differences between init-only and Setter private. We will use as a basis the class below.

public class Pessoa
{    
    public string Nome { get; init; }
    public string Sobrenome { get; private set; }
}

The property with the modifier init cannot have their value changed even within class Pessoa. In this case the compiler issues the CS0272 error.

public class Pessoa
{ 
    // ...
     
    void MudarNome(string novoNome) 
    {
        // Isso é inválido! 
        Nome = novoNome;
    }

    void MudarSobrenome(string novoSobrenome)
    {
        // Isso é válido!
        Sobrenome = novoSobrenome;    
    }
}

In addition, as stated earlier, the property init-only can be initialized during the creation of an object. The property with Setter private cannot have its value set like this, if you try to do this the compiler will issue the CS8852 error (I could not find the link to this error in documentation).

var objeto = new Pessoa
{
    Nome = "Matheus",   // Isso é válido
    Sobrenome = "Silva" // Isso não é válido
}; 
  • 1

    It is good to note that in this answer I talk about the differences between the utilizing of the modifiers. If we are talking about the generated IL the init is much more like the set (he is indeed a method Setter - I even think he’s a Setter public which has access controlled at compilation level only).

  • good response, then the private set is something that can only be modified in the private scope of the class, whether in construction or not, and the init only by instantiating the object is this?

  • That’s it right there.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.