Immutable objects in C# 9

Gal Ilinetsky
CodeX
Published in
3 min readOct 13, 2022

There are cases where we want to use immutable objects rather than mutable ones. Immutable objects are thread-safe, but on the other hand, in many cases, they can cause performance issues (especially in GUI applications). The most popular immutable object that every developer uses on a daily basis is String.

Immutability in C# so far:

Generally, immutability is something that you need to pay for in C#, as it is a mainly mutable language by default. If we want to opt into immutability, we can have some constructs like read-only or non-setters methods for the object’s properties.

For Example:

Immutable class in C# 8

In this case, we are not able to use object initializer syntax. This will force us to long constructors, adding cost to the boilerplate code of types. “Changing” the object’s properties can be done by creating a new method that returns a new instance with the desired changes to the properties. Creating a new method is more code we need to maintain. Basically, it means we have to choose between the ease of use or immutability.

The new Features in C# 9:

Record:

C# 9 introduces records, a new reference type that you can create instead of classes or structs. Records are distinct from classes in that record types use value-based equality

Records are immutable by default, so if we want to create an immutable object, we just need to declare it as a record. This can be done in one line of code as you can see in the following example:

Record definition
Creation of employee record, and a failed attempt to change one of its properties

“Changing” the immutable record’s properties can be done by with expression. With produces a copy of its operand with the specified properties and fields modified:

“Change” record value by creating a new instance with a different Name property value

Records can be mutable too. To convert a record from an immutable to a mutable type, we need to do the following:

Mutable record

Init:

Init properties are a lot like setters, but they can only be used during the construction phase of the object. Init properties, meaning they can be set in the constructor or using a property initializer. That means the object’s immutable properties can participate in object initializers, thus removing the need for all constructor boilerplate in the type.

In this example, you can see that we have a Department class with init properties and we don’t have to create a complicated constructor, as we can use object initializer syntax.

Class with init properties

When we try to change the init property after the object was created we will fail, as we are out of the construction phase of the object.

This way we get an object that is completely immutable, or only with some immutable properties, depending on our purpose.

--

--

Gal Ilinetsky
CodeX
Writer for

Software Engineer, .net development focus. Here to share my knowledge on points of view on software development fields I take interest in.