CSharp9 InitOnlyProperty NewFeature

#CSharp9
#InitOnlyProperty
#NewFeature

If you follow the news, you must know that Microsift Build 2020 has just been completed, and the introduction of C# 9 features was among the new things that were introduced in this build.

First in the list of C# 9 features
Init-only properties
It is obvious, but let’s see what this feature is and why it was added to C#

You should be familiar with the following definitions from the beginning
Immutable type:
An Immutable type is a type whose properties can only be set when it is created and cannot be changed after that.

immutable property:
Simply, an immutable property is a read-only property.

C# allows you to write positional and nominal code.
Constructor method can be used to use positional syntax and Object initializer is also a nominal syntax.

Before C# 9, the use of nominal syntax to initialize objects had a limitation that properties must be writable so that they can be set when creating objects using nominal syntax.

Positional Creation
This coding style is a traditional and old style that has been used since the beginning of C#’s life, where we use the constructor method to initialize the properties of an object.
And when instantiating the class, we must pass the necessary parameters through the constructor method to initialize the object.
“`
public class Person
{
public string FirstName { get; }
public string LastName { get; }

public Person(string firstName, string lastName)
{
firstName = firstName;
lastName = lastName;
}
}

public class Racer : Person
{
public string RacingTeam { get; }

public Racer(string firstName, string lastName, string racingTeam)
: base(firstName, lastName)
{
RacingTeam = racingTeam;
}
}

Person p = new Racer(Charles, Leclerc, Ferrari);
“`
Nominal Creation
By using this coding style, you write less code and instead of defining the constructor method, you use the object initializer feature.

public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}

public class Racer : Person
{
public string RacingTeam { get; set; }
}

Person p = new Racer { FirstName = Charles, LastName = Leclerc, RacingTeam = Ferrari };

However, the use of object initializer has some limitations, despite its convenience and convenience compared to Positional, but behind the scenes, the values ​​of the properties are set after the completion of calling and executing the constructor method of the class, and thus using You cannot define an immutable type from this coding style, and for this purpose you must use the previous style (Positional Creation).

All that has been said so far was an introduction to examine the reason for the introduction of the Init-only property in C# and why this need was felt in C#.
In this way, Init-only properties were introduced so that we can set values ​​and define immutable properties and immutable types when using the object initilizer.
For this purpose, a new accessor called init has been added, which is another form of the previous accessor set.
When initializing an object using the object initializer, the properties defined with this accessor are set when the constructor method of the class is executed, and after the object is created, they will be readonly and cannot be assigned a value.
Validation assigned value to init-only properties

Sometimes it is also required to validate the value assigned to the init-only property.
The point is that you cannot validate a single field for each init-only property, and you must validate it as a blog code for init-only properties.

public class Person
{
public string FirstName { get; init; }
public string LastName { get; init; }

init
{
if (FirstName.Length + LastName.Length > 52)
{
throw new Exception(.);
}
}
}

source
@FullStackDevs

This post is written by MehdiSheykhveysi