What’s new in C# 9
By: Akif Faruk Nane
Tags: Csharp9, Csharp, Dotnet 5, Microsoft

Hey everyone! How are you all doing? I hope you are having a nice and healthy week.

Yesterday, Microsoft Build 2020 event started and we got pretty good news about what will be in C# 9 in the future. In this version of C# language, Microsoft has mostly worked on simplifying expressions and immutable data management. We’ll see pretty good simplified expressions such as switch and new statements which I think will be helpful for programmers and data scientist working with C#. Also, there will be a new feature of python-like coding. That will ease to demonstrate quick examples and be a nice feature for data scientists as well. Speaking of data science, I also have a project here, you can check it if you want. Not to forget, C# 9 will ship with .NET 5.

Now let’s take a look at what’s new in C# 9.

Switch Statement

A little improvement by removing annoying ‘_’ marks.

Directly Comparability in Integers. Switch statements can be constructed by integer comparisons as well. In C# 8, we wouldn’t be able to compare an integer in the switch statement.

And, Or, Not Pattern Combiners.

These new keywords help us combine expressions in switch statement.

Example of And Keyword

Example of Not Keyword

Python-Like Coding

We won’t need to specify program class and also static void main . We can start writing code immediately as if we are in the main function, like below. Creating a demo code will be much easier than ever.

and also . We can start writing code immediately as if we are in the main function, like below. Creating a demo code will be much easier than ever. Magic parameter args will be available to be used just like value parameter in a setter method.

Init Feature

Init method will replace the setter method. It will allow us to change the value of a field in only the initialization stage which is in the curly brackets after the new statement. Subsequent changes in fields will throw compile error.

Data | Record Type

There is a new type of class named data currently. It makes a class containing data keyword, immutable. The keyword name might be changed to record in the future as the developer team says. It has a few features comes with. Let’s look at how we can define data type classes first.

The default access modifier is public in data classes. We can still define private fields as well.

There is this new keyword with which helps us to modify a data object by automatically cloning it to a new instance. I think It will save a lot of time instead of hand cloning everything. In the example below, there are two instances whose names are the same, last names are different.

We can also destruct a data instance easily into variables.

The developer team of C# says there will be inheritance for data type classes. It’s now uncertain how they are going to implement it. Here is a few shot of what they showed in the event. It is similar to what we do for normal classes.

Other C# 9 Features

Target-typed new: meaning that we don’t have to specify the type when using new keyword if the target type is already defined in the variable side. That’s a cool feature.

We had already something similar to this which is defining arrays in C# 8, but in this case, it determines the target-type from the elements inside the array, not from the type at the variable side. So, that’s a different case from what they bring now.

Parameter null checking: This helps us checking if the parameter is null. If a parameter is null, it throws an error automatically. It’s now uncertain what mark they will use. The current idea is to use “!” mark.

Covariant Returns: The new override feature helps us to change the output type of a function. In some cases, for example we know that a function will return Student object for certain. However its return type is Person. What we have to do normally is call that function and cast the return value to the Student type explicitly. Now with this feature, by overriding the function with Student type, we don’t have to do this. It will automatically return Student type value.

Conclusion

I see the developer team of C# is working hard on implementing those features. The new features of C# 9 are exactly what we needed in C#, helps us getting out of those strict rules of a strongly typed language. I’m happy that C# is strongly typed language, but in some cases we lose a lot of time by writing unnecessary expressions and checkings. The shortenings are really nice that way. I personally don’t have much experience in immutable data topic, so I can’t make a comment about that yet. Lastly, I loved python-like coding feature. It will enable us to create short demos that’s also what I’m planning for my deep learning project. To be able to create things fast without losing any flexibility and capability is really wonderful.

Note: All of the pictures above are taken and edited from https://channel9.msdn.com/Events/Build/2020/BOD108