init-only properties
04/20/2021
Init-only properties are a new addition to C# 9. The purpose of this blog post is to look into how it is represented in IL, as well to see how it behaves from IL verification's point of view.
What is an init-only property?
Properties, readonly properties and properties with private setter have been around for a while in C#. One use-case was still missing: setting properties with object initializers that are readonly. Previously these properties had to be read-write or values had to be passed through constructors, which involved writing a good set of constructors. The following code snippet shows an example of an init-only property. State
class has a bool
'On' property, which is an init-only property.
var data = new State { On = true }; Console.WriteLine(data.On); public class State { public bool On { get; init; } }