Init-only Properties
.NET 5 is out! And with it, C# 9, which adds a lot of new features to the language. In this post, I'm going to talk about one of my favorites, init-only properties . Immutability Before C# 9 Before C# 9, we can make a class immutable by using getter-only auto properties together with a constructor whose arguments are used to populate those properties: public class Product { public Product(string name) { Name = name; } public string Name { get; } } This is an effective way for making classes immutable. We cannot change the property's value after the class has been instantiated: var product = new Product("Cool Product"); product.Name = "Cooler product"; // compile error We even can't change it from within the class: public class Product { public Product(string name) { Name = name; } public string Name { get; } public void SomeMethod() { Name = "Cooler product...