Using Init-Only Properties In C# 9.0

Introduction

 
In today’s article we will look at Init-only properties how they work and what are the advantages to having them. This is a new feature in C# 9.0. 
 

What are Init-only properties?

 
Microsoft recently announced the availability of .NET 5 release candidate at Microsoft Ignite 2020. This included the latest features in C# 9.0. In order to code in .NET 5.0, we would need to install the latest preview of Visual Studio 2019 (Version 16.8.0 Preview 3.1). As I had read about some cool features in C# 9.0including Init-only properties, I downloaded and installed the required version as below,
 
Using Init-only properties in C# 9.0
 
We have all created immutable classes in previous versions of C#. The way to set values of properties in these immutable classes is by passing values via the constructor.
 
This is where Init-only properties come in. By using them, we can set values at the time of creating the class instance. However, this is the only time we can set these values.
 
After that we are not allowed to change the values. Hence, the class is immutable without us having to create a constructor just to set values of the properties.
 

Using Init-only properties

 
Let us see them in action.
 
Let us create a console application in Visual Studio 2019 (Version 16.8.0 Preview 3.1) as below,
 
Using Init-only properties in C# 9.0
 
Using Init-only properties in C# 9.0
 
Using Init-only properties in C# 9.0
 
Let us look at the properties of the project. These are as below,
 
Using Init-only properties in C# 9.0
 
Now, we enter the below code,
  1. using System;  
  2. namespace ConsoleAppInit {  
  3.     class Program {  
  4.         static void Main(string[] args) {  
  5.             var employee = new Employee(1, "John Smith", 30);  
  6.             Console.Write($ "Employee details: Id={ employee.ID}, Name={employee.Name}, Age={employee.Age}");  
  7.             Console.ReadKey();  
  8.         }  
  9.     }  
  10.     public class Employee {  
  11.         public int ID {  
  12.             get;  
  13.             private set;  
  14.         }  
  15.         public string Name {  
  16.             get;  
  17.             private set;  
  18.         }  
  19.         public int Age {  
  20.             get;  
  21.             private set;  
  22.         }  
  23.         public Employee() {}  
  24.         public Employee(int id, string name, int age) {  
  25.             ID = id;  
  26.             Name = name;  
  27.             Age = age;  
  28.         }  
  29.     }  
  30. }  
When we run the application, we get the desired results as below,
 
Using Init-only properties in C# 9.0
 
However, we have set values of the immutable class by using a special constructor. If we try to add values directly at the time of creation of the class instance, we get the below,
 
Using Init-only properties in C# 9.0
 
Next, we will change the properties of the project and set the Target Framework to .NET 5.0, as below,
 
Using Init-only properties in C# 9.0
 
Now replace your code with the below,
  1. using System;  
  2. namespace ConsoleAppInit {  
  3.     class Program {  
  4.         static void Main(string[] args) {  
  5.             var employee = new Employee {  
  6.                 ID = 2, Name = "Jane Smith", Age = 31  
  7.             };  
  8.             Console.Write($ "Employee details: Id={ employee.ID}, Name={employee.Name}, Age={employee.Age}");  
  9.             Console.ReadKey();  
  10.         }  
  11.     }  
  12.     public class Employee {  
  13.         public int ID {  
  14.             get;  
  15.             init;  
  16.         }  
  17.         public string Name {  
  18.             get;  
  19.             init;  
  20.         }  
  21.         public int Age {  
  22.             get;  
  23.             init;  
  24.         }  
  25.     }  
  26. }  
In the above code, we see that we specify the properties as get and init. This means that a value can be read from the property and can only be set at the time of creation of the class instance. Hence, the class is immutable, but we did not need to create a special constructor for it.
 
We can execute the code and confirm all works fine,
 
Using Init-only properties in C# 9.0
 
If we try to change a value after initial creation, we get an error as below,
 
Using Init-only properties in C# 9.0
 
In the past declaring a class as immutable was not very declarative. We had to create a special constructor. With Init-only properties the process is clearer as we clearly declare the property with the init keyword. This makes our code better to understand and shows our intention that we are declaring a class as immutable.
 

Summary

 
In this article, we looked atInit-only properties which are a new feature in C# 9.0. The main advantage I see is that it is now clear for us to communicate the fact that we are declaring an immutable class. Happy Coding!