Within this guide, you will learn about the most useful new features within C# 10 that will make your development life easier. Keeping up with the latest releases of software can be hard and if we are honest, most developers don't have the motivation to read through the release notes for every new update.

I do not think this lack of motivation is due to laziness. I personally find that the majority of new framework features that get released tend to be in areas that I may use once in a blue moon. It takes a lot of effort to separate the good from the bad features. This is where this guide will help 💥

Instead of having to spend hours reading through all the Microsoft documentation, this guide will teach you about the features that I think you will find most useful within C# 10 and ASP.NET 6. If you want to level up your C# game as effortlessly as possible, this is the tutorial for you 🔥🔥🔥

File-scoped Namespaces

Within C#, a namespace is used to help improve organisation within your classes. Traditionally, a namespace is declared at the top of a class. All code that is scope to the namespace would then be added in between a set of curly braces, like this:

Obviously, it takes hours of effort to type a { or a } and nowadays we are all far too busy to type characters on a keyboard 🤔🤔🤔

Joking aside, the biggest issue with namespaces is the additional indentation. Having to use indents for your namespace adds an extra level of nesting to your classes. With file-scoped namespaces, you can get rid of that additional horizontal clutter within your classes. Using the new file scope namespace feature, you can define your namespace while omitting the curly braces!

File-scoped namespaces are a handy trick that will help clean up your classes and save you from wasting time worrying about indents. Over the years, I must have wasted hundreds of hours simply adding and removing tabs to my classes. Anything that can relieve this pain is a win for me ✔️

Global Using Statements

Comparing all the new features from C# 10, I think that the new global using statement feature is probably the one that will save you the most amount of time.

How much time have you wasted, refactoring and removing any unused using statements from the top of your code classes?

Cleaning your using statements is a needed evil, however, this management takes time and effort. Granted you can use a tool like Resharper or CodeMain to automate this, however, if you have code OCD then I often find myself still manually cleaning classes.

One way to reduce this code maintenance burden is to use global using statements. The global using feature will help eliminate the number of using statements you need to include on a per-class basis. This feature will allow you to define a using statement once and then all the project classes will automatically have access to them.

One important point to mention is that global using statements are namespace scoped, meaning a global using will only be applicable to work at the project level and not the solution level. If you want to use this feature solution-wide you will have to redefine any solution-wide global using statements per project/class library.

In terms of best practices, from .NET 6 onwards it is recommended to create a specific class to handle all of the project global using statements. You could name this class something imaginative like GlobalUsing.cs and it should live in your project's root directory so it is not scoped to a specific namespace. The file would look something like this:

Within GlobalUsing.cs you are free to add whatever references you want to be shared across all of your project files. On top of being able to add your own global using, within ASP.NET 6, certain default using statements are also automatically included. These references include System, System.Collections.Generic, System.IO, System.Linq, System.Net.Http, System.Threading, and System.Threading.Tasks

You can prove this behind-the-scenes magic yourself. After you build your project, look within obj/Debug/net6.0. Here you should see a class called YOUR-NAMESPACE.Globalusing.cs. open that file are you will see something like this:

Within this example, I have not intentionally added any of the System using statements! The great thing about this is that it removes vertical clutter from your classes. You can see more of your class's logic without having to scroll.

Constant interpolated strings

The next feature is not as groundbreaking as the previous two, however, it is a nice handy inclusion to the framework. When interpolated strings were released in C# 6, developers had an easier way to construct strings in code. Interpolated strings allowed developers to easily combine text and variables using a single command.

One limit to this feature was around constants. If you defined a string with the const modifier, you could not make use of an interpolated string. This has now been rectified and within C# 10 it is now possible to write code like this:

The only caveat when using constant interpolated strings is that any variable you use within it needs to be a constant. For example, this code will throw a compiler error:

Deconstruction improvements

Previously in C#, you had two ways to use deconstruction. Let us say we have this method:

This method returns two strings within the same method call. Within C# 9 and below, you could write some code like this to access those values:

Alternatively, you could also write code like this:

You could only deconstruct values using one of the techniques. It was not possible to mix and match declarations and expressions within a single deconstruction. From C# 10, this limitation has now been removed and you are now free to write code like this:

This change is not groundbreaking, however, it will result in some slightly nicer code 😊

HTTP Logging

When building my first websites with .NET 5 it was slightly annoying to find out there was no in-built process for logging. If you wanted to find out why a controller was not working, you needed to install an additional library. Typically I used Serilog for logging, as I could create logs in JSON which provides an easier way to query for issues. I even recorded a video on this subject here

When creating proof of concepts or small apps, adding in additional libraries seemed like a hassle. The good news is .NET 6 introduces HTTP Logging middleware for ASP.NET Core apps. To enable this feature within Program.cs (or Startup.cs if you have one). it is now possible to write some code like this to enable logging:

It is also possible to customize the logger using AddHttpLogging() like this:

Improved Property Pattern Matching

Over the last few years, the pattern matching capabilities within C# have continued to improve. In C# 8 we got the property pattern matching capabilities. This feature provides you with the capability of searching for values contained within objects. A property pattern is defined with {}. Below shows a simple example:

Within C# 10 this feature is levelled-up up with extended property pattern capabilities. One previous limitation with this pattern matching was nested objects. If you had an object, that contained a property that referenced another object, the syntax was slightly long-winded. Imagine we had to work with this object:

To do some property matching on the child object, you ouild write this syntax:

With the new enhancement, you can now target child properties directly, like this:

The extended property pattern allows for matching on these sub-objects with slightly easier syntax 😎😎😎


You are now a C# 10 master. If you can learn these features and get into the habit of using them, your code will benefit. Happy Coding 🤘

https://www.syncfusion.com/blogs/post/5-features-in-c-sharp-10-every-developer-should-know.aspx