C# has been around for a while. Since January 2002, to be more precise. As we wait for the 20th birthday of this popular programming language, we got its new version as well. C# 10 is released in 9. November along with the .NET 6. If you follow its GitHub page, you can find a lot of features and ideas that are suggested for the new version of the language.
This bundle of e-books is specially crafted forย beginners.
Everything from Python basics to the deployment of Machine Learning algorithms to production in one place.
Become a Machine Learning Superheroย TODAY!
This can be a bit overwhelming, however, if you follow Microsoft conferences and listen to what some of the C# designers are saying and cross-check it with suggestions on the Github page, you can get a nice picture of what is coming next for C#. For example, you can check out this Microsoft Build conference from May and you can find a lot of interesting information. Here are five new features that we will see in the new version of C#:
1. 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.
1.1 Current 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:
public SomeFunction(int id, SomeClass newObject)
{
if (newObject == null)
{
throw new ArgumentNullException("newObject");
}
...
}
In the code above, we inject newObject of SomeClass into SomeFunction. If the mentioned object is null we should not proceed with the code. I know a lot of people will say that with the correct application design this problem should not exist in the first place, however, sometimes it is necessary to do this.
1.2 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:
public SomeFunction(int id, SomeClass newObject!!)
{
...
}
In this example, the code will automatically check if newObject is null. The ArgumentNullException will be automatically thrown if the newObject value is null.
2. Extended property patterns
Sometiemes we need nested properties in our code. Meaning property of a property. Using pattern matching on these values is posible in previous versions of the C# as well, but the code is kinda ugly.
2.1 Current Problem
Here is what code for switch looks like in C#9:
if (e is MethodCallExpression { Method: { Name: "MethodName" } })
Bah, a bit wierd. It would be easier to just use the dot in patter matching.ย
2.2 C# 10 Feature
C#10 introduces exatcly the improvement that is necessary. Instead of repeating brackets several times, now the code from above is more intuitieve:
if (e is MethodCallExpression { Method.Name: "MethodName" })
3. 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.ย
3.1 Current Problem
In previous version of C#, this keyword was reserved for classes.ย
public record Person(string FirstName, string LastName);
3.2 C# 10 Feature
In C#10,ย records are taken one step fruther. 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:
public readonly record struct Point(double X, double Y, double Z);
4. 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.
4.1 Current Problem
If we take for example one ASP.NET file, the list of usings is huge:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
4.2 C# 10 Feature
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.
global using Microsoft.AspNetCore.Builder;
global using Microsoft.AspNetCore.Hosting;
global using Microsoft.AspNetCore.HttpsPolicy;
global using Microsoft.AspNetCore.Identity;
global using Microsoft.AspNetCore.Identity.UI;
global using Microsoft.EntityFrameworkCore;
global using Microsoft.Extensions.Configuration;
global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.Extensions.Hosting;
global using System;
global using System.Collections.Generic;
global using System.Linq;
global using System.Threading.Tasks;
5. File Namespaces
Using the global keyword C# eliminates vertical waste and with file namespaces it eliminates horizontal waste. Basically, this feature aims to solve the indentation problem and it is more of a cosmetic change that will hopefully make your code cleaner.
5.1 Current Problem
Nowadays, after the list of usings, the first thing you see in a C# file is the definition of the namespace. After that comes the definition of the class. The implementation of the class is already indented at this point. This is what we consider horizontal waste.
namespace Blog
{
public class Article
{
...
}
}
5.2 C# 10 Feature
With this feature you can define the namespace on the file level like this:
namespace Blog;
public class Article
{
...
}
Conclusion
In this article, we had a chance to get familiar with 5 new features that the new version of C# will bring. To be honest I can’t wait to try them all out. Some of them seem like a major improvement and it seems that it will affect how we organize and write C# projects.
Thank you for reading!

Nikola M. Zivkovic
Nikola isย the author of books:ย Ultimate Guide to Machine Learningย andย Deep Learning for Programmers. He loves knowledge sharing, and he is an experienced speaker. You can find him speaking atย meetups, conferences, and as a guest lecturer at the University of Novi Sad.
thanks, thanks!
You are welcome, glad you liked it!
This is very helpful. If you have done articles on previous versions of C# it would be great to have a way to access those as well.
HI David,
Thanks, I am glad you liked it.
I have written the simmilar article for C# 8 a couple of years ago – https://rubikscode.net/2017/10/23/c-8-the-shape-of-the-things-to-come/
I skipped C# 9 for some reason.
Can a using alias be made global?
global using Map=System.Collections.Generic.Dictionary;
That would be more useful that the global namespace using.
Hey Zames,
Thanks for the comment.
I think not I haven’t seen in in the specs for it.
https://github.com/dotnet/csharplang/blob/main/proposals/GlobalUsingDirective.md
Visual Basic .NET has had global usings (a.k.a. imports) since its inception in 2003. Though they are set on the project level, not a file level the idea is the same. It’s about time.
I like the idea of specifying a single line for the namespace.
So this is for C# 10 as the title says. Is this with .NET 6 or will these features be available in .NET Framework (i.e. .NET Framework 4.7.2.) Or are these feature more specific to the Visual Studio version you are using (i.e. Visual Studio 2022 – maybe Visual Studio 2019 with a specific update?)
Hi Kris,
Thanks for the comment.
I think it is a neat feature as well.
C# 10 doesnโt have an official release date yet, although it will probably be released in November along with the .NET 6. I am not sure if this will be a part of .NET Framework, I will try to find out.
First of all, the blog looks awful on a mobile.
Second, I can imagine a tool that will take most used namespaces plus all namespaces in the solution and create a usings.cs. Would that impact performance of code or Visual Studio?
I like the unindented namespace thing.
Hey Siderite,
I am sorry to hear that. Can you tell me in which setup you experience the problems? Thanks in advance.
Regarding the performance, I think it will be translated during the compile time, so it will not affect the performance of the code. I can understand your worries that InteliSense in Visual Studio might have problems with globals, however, I am sure that this will be handled ๐
Thank you for writing this article, and particularly for the “x.1” and “x.2” technique (current problem, feature). Many articles just show the feature, and try to describe in words what problem is being solved. Your way of writing this makes it very easy to understand the use-case for the feature.
You are welcome, I am glad you like it ๐
Thanks for this. Great writeup on some useful features.
Glad you like it!
Thank you! This is very helpful.
Thanks, glad you like it!
Very well explained. Thanks for the hard work.
Thanks! Glad you liked it!
As for the features, really not a fan of the “global” using, anything global is a bit yucky imho and in this case I dont think it brings a lot of value, with modern IDEs you tend to let the IDE bring in using for you, and highlight the ones you no longer need.
granted not everyone uses an IDE but this isnt the way I would solve this problem. It would be much nicer to create global “groups”. This way if there are a lot your using, at the top of the file you could put
“using GlobalBoilerPlate”
at the top of your file to perform the above.
heck id even prefer this feature if you had to specifically put “using global” at the top. Its the idea that it silently injects usings through your codebase that I really dont like.
Also groups might encourage people to actually only use the stuff they need. For example say you see this:
“using standardMicrosoftStuff”
“using standardNewtonSoftStuff”
You might go, oh I actually don’t use NewtonSoft in this file, and cut back on one using ๐
also i’m starting to feel like modern languages need to get calm down on the cryptic “symbols” for everything why was “!!” preferred over say autoNullCheck or some descriptive keyword? At the very least it should surely be ?? to match the null check symbols. As much as im a fan of concise syntax, stuff should be readable or semi intuitive.
Yes, I can see how it can get messy with global usings. Completely agree. It should have proceed with causion sign on it ๐
Groups is not a bad idea, but could have a simmilar impact as global usings. It contains the risk that someone will define the group in a wrong way.
I tried globals with ML.NET on one project and for that specific project it turned out usefull, because it was just redundant writing using Microsoft.ML everywhere.
Excellent written!
Captures quick and easy the important new things. Itโs gets a bit easier for each new version. Especially namespace thing is good. I havenโt yet fully understood the good about records vs using a class, but will read more on that ๐