Posts C# 10.0 - What's new
C# 10.0 - What's new
Cancel

C# 10.0 - What's new

It is November again which means that Microsoft released a new version of .NET. This year, Microsoft released with .NET 6 a long-term support (LTS) version, and guarantees support for .NET 6 for 3 years. Along with .NET 6, Microsoft published C# 10.

In this post, I will look into some of the new features of .NET 6 in combination with C# 10.

You can find the code of this demo on Github.

Prerequisites for .NET 6 and C# 10

Before you can use .NET 6 and C# 10, you have to download Visual Studio 2022. You can use the Community edition free of charge. This version offers the same features as the Professional one but does not allow you to use it for commercial projects.

Removing Boilerplate code

When you create a new console application, you are probably used to seeing the Program.cs file that contains some usings, a namespace, the Program class, and the Main method which writes something to the console. This is a lot of code for a single console output.

The old console application template

The old console application template

Microsoft thinks that this is way more complicated than it has to be and removed all of that and reduced the program.cs file to a single line that writes a message to the console.

The default console application template

The default console application template

Another reason why Microsoft removed all the boilerplate code is to make it easier for beginners to get started. This might be true but it also hides a lot of basic knowledge that beginners should learn early on. Additionally, if you want to add a method, you have to add the class and Main method manually.

All in all, I understand Microsoft’s decision but I am not a big fan of it yet. Perhaps this will change in a year when I got used to it. Time will tell.

Global Usings

Global usings is probably my favorite feature of C# 10 since it allows developers to create a single file and add usings there. These usings will be added automatically to every file in the project. This should reduce the number of usings in each file and will help to make the files clearer.

Let’s look at some code. In the Program.cs file, initialize a new variable from a class.

Note that you don’t have to add a using for this class. The NullParameterChecking class looks as follows:

As you can see in the code, you do not have to add brackets after the namespace. This is also a new feature and will help to reduce the indentation of the code.

Lastly, create a new file that will contain the global usings. The name of the file does not matter. I prefer naming it GlobalUsings.cs. This file contains all usings that you want to apply globally:

Record Structs

C# 9 already introduced records for classes. C# 10 improves this feature and adds records for structs. You can define a struct with a single line and the compiler will automatically create properties according to the parameters of the constructor.

The Coordinate struct has three properties, X, Y, and Z. You can create a new struct and then access these properties the same way as if you had defined them yourselves.

Null Paramter Checking

Every developer knows that we should check all parameters for null but it is a tedious task. Especially if your method has several parameters it is quite a lot of typing for such a simple task. C# 10 can create these checks almost automatically for you. All you have to do is to add one ! after the parameter and C# 10 (maybe it is Visual Studio though) will create a null check that throws an exception if the parameter is null.

Conclusion

This post gave a short introduction to the new features of C# 10 and .NET 6. C# is a quite mature language therefore the changes are not that massive anymore. Maybe I am a bit slow to adapt to the new changes but so far I am not too excited about them. Allowing us to use global usings is a nice feature and also the automatic null checks of parameters will boost productivity. The other features have to prove themselves when I start using them in a real-world project.

In my next post, I will take a look at minimal APIs which is probably Microsoft’s most talked about feature of .NET 6.

You can find the code of this demo on Github.

This post is licensed under CC BY 4.0 by the author.