What's new in C# 10

C# 10 was released in November 2021 alongside Visual Studio 2022 and .NET 6. It adds several new features which make it easier and more convenient to work with. Here’s a tour of some of the most useful additions and how they’ll enhance your workflow.

File-Scoped Namespaces

Let’s start with one of the simplest but perhaps most significant changes. You can now apply a namespace to an entire file via a new syntax option for the namespace keyword. The remaining code inside the file will be automatically namespaced, even if it’s not indented inside a namespace block. This works similarly to namespace declarations in PHP.

In C# 9, you needed this code to namespace a class:

In C# 10, you can use the following instead:

This saves you some horizontal space in your editor by cutting out a level of indentation. If the file needs to contain multiple classes, you can write them all beginning at column zero – the namespace declaration applies to the whole file.

 

Global Usings

This is a new feature that will simplify the code a lot. As you are aware, every C# file starts with a list of usings that are necessary for the implementation. However, sometimes this is redundant. Especially if you are working with ASP.NET a lot of that code is repeated and in general just a noise for coders.

Another keyword that C# 10 introduces is global. Using global, you will be able to define global usings for the whole project. In general, it is recommended to create a separate file which will contain these imports, something like usings.cs.

This means that other files in the project can be simplified since they no longer need to add all these usings. C# designers refer to this as the elimination of vertical waste.

 

 

Null Parameter Checking

Null Reference Exception is one of the worst bugs that you can have in your code. It is just painful. In order to protect from this type of exception, you need a bulletproof design of the application and a lot of checks for parameters of the functions. This new feature should help us with that and make our code more readable and robust.

Problem: At the moment if you want to check a certain parameter of your function is null, you would have to do something like this:

C# 10 Feature: The new version of C# aims to simplify this problem for us with – !!. All you have to do is add two exclamation points after the name of the parameter:

 

 

Extended property patterns

Sometimes we need nested properties in our code. Meaning property of a property. Using pattern matching on these values is possible in previous versions of the C# as well, but the code is kinda ugly.

Problem:  Here is what code for switch looks like in C#9:

C# 10 Feature:  C#10 introduces exactly the improvement that is necessary. Instead of repeating brackets several times, now the code from above is more intuitive:

 

Record Struct

Record keyword for classes was first introduced in C#9. This keyword is used for reference types that provide built-in functionality for encapsulating data. Records are primarily intended for supporting immutable data models. 

Problem: In the previous version of C#, this keyword was reserved for classes. 

C# 10 Feature:  In C#10, records are taken one step further. Now, you can also use structure types to design data-centric types that provide value equality and little or no behavior. In C# 10 and later, you can define record struct types:

 

Lambda Expression Enhancements

C# 10 adds several improvements to lambda expressions covering their types and syntax. The objective is to make lambdas more akin to regular methods and local functions. Defining one will now be a more familiar experience.

The concept of a “natural” type has been introduced. This lets the compiler infer a lambda’s type without manually converting it to a delegate or expression. This creates more readable code when assigning a lambda to a variable:

 

 

Conclusion

These were the main features of C# 10. To see all the features and changes from the official website here.