What is difference between Init-Only and ReadOnly in C# 9?

As stated in the new C# 9 features post,

The one big limitation today is that the properties have to be mutable
for object initializers to work: They function by first calling the
object’s constructor (the default, parameterless one in this case) and
then assigning to the property setters.

However, value types with readonly modifiers are immutable as stated in readonly documentation.

Therefore, it is not possible to use readonly properties with object initializers.

However, with Init-only properties you can use object initializers.

Leave a Comment