Some words about C# 9.0

Some words about C# 9.0

In my last blog post, I introduced .NET 5.0 whose one of the more important things is C# 9.0. Today I would like to write more about it. C# 9.0 is called as a version that focused on program simplicity and data immutability. Among other things, there appeared a new kind of types – Records or a feature called Top-level programs. Let’s move on to the details!


I guess it is the most popular and interesting feature that contains in the new version of C#. As I mentioned earlier, it is a new kind of types. Records are reference immutable types. What does it mean? Well, it is a class whose change creates a new object in memory. When can it be useful? In a situation where we do not want to change the object, for example, in multi-threaded programming. This is an example record:

This is how creating an object looks like:

The output of our program is displayed in the following screenshot:

As you can see in the listing, records support with expression that is used for creating a copy of a record. Of course, records give you a possibility for inheriting:

And for confirmation, the result of the above code looks like this:

Top-level statements

Top-level statements are a feature that reduces the number of lines of code in the Program class. This is a cool novelty, each such simplification is valuable. For example, the Program class you see above might look like this:

Init only setters

Sometimes you need to make a read-only property. In previous versions, to get this you just didn’t include the set keyword in the declaration. However, you were unable to set property values in the braces right after calling the constructor. In the new version, the Microsoft development team decided to introduce Init-only setter. How to use it? You just add the init keyword in properties. This gives you a read-only property that you can set later when creating a new object instance. It looks like this:

Pattern matching enhancements

C# 9.0 offers more manoeuvring space with regards to create logical conditions. There are appeared new patterns such as and, or, not. Additionally, you can use relation patterns (less than, equal etc.) which greatly facilitates the creation of conditions. Take a look at the following example of using these novelties:


In my opinion, the features introduced in the new version are really worth attention and may turn out to be useful for programmers in their daily work. If you want to see more examples and delve into this topic, I recommend you watch the videos from .NET Conf 2020.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top