Wednesday, September 15, 2021

Using Init-Only Properties In C# 9.0

 What are Init-only properties?

 
Microsoft recently announced the availability of a .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 bypassing 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 the 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 the 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.

No comments:

Post a Comment

How to register multiple implementations of the same interface in Asp.Net Core?

 Problem: I have services that are derived from the same interface. public interface IService { } public class ServiceA : IService { ...