C# 9.0 - Introduction To Init-Only Property

Introduction

 
In this article, I am going to explain Init-only setter property which has been introduced in C# 9.0.
 
Each C# release has its own features. In C# 9.0, many new wonderful features have been added and here we are going to discuss important feature Init-only setter property.
Before we start, we should have basic understanding of immutable and mutable in c#.
 
What’s meaning of Mutable and Immutable in English?
 
Answers are:
  • Mutable – Can change
  • Immutable – Cannot Change
In simple words we can say, immutable means object cannot be changed once it is created. If you want to change, you have to create new object/ assign new memory. Example of immutable type is string in C#.
 

What is Init-only setter property?

 
Before we start with Init-only property, we have to understand the traditional way of working (properties).
 
Let’s start with an example,
 
Suppose, we have Member class which has 3 properties, 
  1. ID - Member unique ID
  2. Name - Member Name
  3. Address - Member Address
  1. public class Member  
  2. {  
  3.     public int Id { getset; }  
  4.     public string Name { getset; }  
  5.     public string Address { getset; }  
  6. }  
Now create object of Member class and assign some values,
  1. using System;  
  2.   
  3. namespace C_9._0  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             Member memberObj = new Member();  
  10.             memberObj.Id= 1;  
  11.             memberObj.Name="Kirtesh Shah";  
  12.             memberObj.Address="Vadodara";  
  13.   
  14.             Console.WriteLine("****************START - Member Details***********");  
  15.   
  16.             Console.WriteLine("ID : -" + memberObj.Id);  
  17.             Console.WriteLine("Name :- " + memberObj.Name);  
  18.             Console.WriteLine("Address:- " + memberObj.Address);  
  19.             Console.ReadLine();  
  20.   
  21.             Console.WriteLine("****************END - Member Details***********");  
  22.         }  
  23.     }  
  24. }  
All looks good right? But what happens if someone changed Member  ID? ID is unique and it should not be editable, which means it should be immutable, not mutable. That's the problem with the traditional way, properties are mutable.

If we want to make properties immutable in the traditional way, we have to pass values in constructor like the below code:
  1. public class Member  
  2. {  
  3.     public Member(int memberId)  
  4.     {  
  5.         Id = memberId;  
  6.     }  
  7.     public int Id { get; }  
  8.     public string Name { getset; }  
  9.     public string Address { getset; }  
  10. }  
ID property is immutable now.

Let's see the below code:
  1. using System;  
  2.   
  3. namespace C_9._0  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             Member memberObj = new Member(1);  
  10.             memberObj.Name="Kirtesh Shah";  
  11.             memberObj.Address="Vadodara";  
  12.   
  13.             Console.WriteLine("****************START - Member Details***********");  
  14.   
  15.             Console.WriteLine("ID : " + memberObj.Id);  
  16.             Console.WriteLine("Name : " + memberObj.Name);  
  17.             Console.WriteLine("Address: " + memberObj.Address);  
  18.             Console.ReadLine();  
  19.   
  20.             Console.WriteLine("****************END - Member Details***********");  
  21.         }  
  22.     }  
  23. }  
Output of above code would be,
 
C# 9.0 - Introduction To Init-Only Property
 
Now ID property is Immutable and cannot be changed once object is created. To make mutable property into immutable, we have to create a constructor.
 
In C# 9.0, we can achieve the same thing using Init-only property.
  1. public class Member  
  2. {  
  3.     public int Id { getinit; }  // set is replaced with init
  4.     public string Name { getset; }  
  5.     public string Address { getset; }  
  6. }  
  1. using System;  
  2.   
  3. namespace C_9._0  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             Member memberObj = new Member(1);  
  10.             memberObj.Name="Kirtesh Shah";  
  11.             memberObj.Address="Vadodara";  
  12.   
  13.             Console.WriteLine("****************START - Member Details***********");  
  14.   
  15.             Console.WriteLine("ID : " + memberObj.Id);  
  16.             Console.WriteLine("Name : " + memberObj.Name);  
  17.             Console.WriteLine("Address: " + memberObj.Address);  
  18.             Console.ReadLine();  
  19.   
  20.             Console.WriteLine("****************END - Member Details***********");  
  21.         }  
  22.     }  
  23. }  
We got the same output as in the previous code 
 
C# 9.0 - Introduction To Init-Only Property
 
It is the power of Init-only property. What will we do, if we want to do some validation like ID should not be null or string? Init-only property has the capability to set read only property to implement validations.
 
Suppose we have Member class like below,
  1. public class Member  
  2. {  
  3.     public int Id { getinit; }  
  4.     public string Name { getinit; }  
  5.     public string Address { getinit; }  
  6. }  
Now we will try to create a Member class object like below,
  1. using System;  
  2.   
  3. namespace C_9._0  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             Member memberObj = new Member(1);  
  10.              
  11.             Console.WriteLine("****************START - Member Details***********");  
  12.   
  13.             Console.WriteLine("ID : " + memberObj.Id);  
  14.             Console.WriteLine("Name : " + memberObj.Name);  
  15.             Console.WriteLine("Address: " + memberObj.Address);  
  16.             Console.ReadLine();  
  17.   
  18.             Console.WriteLine("****************END - Member Details***********");  
  19.         }  
  20.     }  
  21. }  
Init-only properties can or cannot be set as per your requirement. As you notice in the above code, only ID property is set and name and address properties are not set. Please note that if any property is not set at the time of object creation that property cannot be set.
 
I hope you enjoyed this article.