I’m going to talk about every single C# 10 features. We will look at them individually with an example.
The top 6 features are listed down.
- Global using directive
- File Scoped Namespace
- Constant Interpolated strings
- Lambda Improvements
- Null Parameter Checking
IDE which I have used for preparing the examples
- Microsoft Visual Studio Community 2022 (64-bit)
- Version: 17.0.5
Global using Directive
This is one of the interesting features. Let’s go ahead and create a Console application in VS 2022 and add the below lines into Program.cs file
using System.Text.Json;
var names = new[] { "Prasad", "Praveen" };
var serialized = JsonSerializer.Serialize(names);
Console.WriteLine(serialized);
This code is perfectly valid in C# 9.0.
Assume that we need to utilize the JsonSerializer.Serialize() method in multiple files under the same project, Till C# 9.0, we have only one option to add the reference System,Text.Json at the beginning of each files. In C# 10, Microsoft has been introduced a new feature to add the reference globally at one place using the keyword “global”. It is recommended to create a separate file which will contain these imports, something like “usings.cs”
C# 10 code would look like below
Program.cs
var names = new[] { "Prasad", "Praveen" };
var serialized = JsonSerializer.Serialize(names);
Console.WriteLine(serialized);
A new file usings.cs will be created and moved all the using imports into it.
Usings.cs
This class file will have only global using System.Text.Json
Assume that I’m going to comment out the line in Usings.cs, if you go back to Program.cs class, you will be getting a compiler error. To resolve this issue, we can edit the project file, and add the <ItemGroup> tags
GlobalUsingDirective.csproj( this is my project file)
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup Condition="'$(ImplicitUsings)'=='true' Or '$(ImplicitUsings)'== 'enable'">
<Using Include="System.Text.Json"/>
</ItemGroup>
</Project>
File Scoped Namespaces
Assume that I have a class file Employee.cs and inside the class, the code looks like below
namespace FileScopedNamespace
{
internal class Employee
{
public string Name { get; set; }
}
}
I’m going to add a new namespace within the same file as below, now my Employee.cs file looks like below since in theory, we can have multiple namespaces in a file.
namespace FileScopedNamespace
{
internal class Employee
{
public string Name { get; set; }
}
}
namespace FileScopedNamespace1
{
internal class Employee1
{
public string Name { get; set; }
}
}
In my 10 years of career in C#, I have never seen such a class file with multiple namespaces. I assume, C# Dev Team also may have same thoughts and what they did, they just removed the pointless nesting of namespace and class and converted into File Scoped Namespace as below
namespace FileScopedNamespace;
internal class Employee
{
public string Name { get; set; }
}
Whatever we are in this Employee.cs file comes under the namespace "FileScopedNamespace;". This is really amazing, and I love this feature.

Matt ArnoldPosted Aug 8, 2022, 3:01 PM
A nice extension to the single statement namespace would be something like `default namespace;` - the thought being that the namespace is usually a mirror of the project's default namespace with the directory path from the project folder to the file being suffixed on the end. This would eliminate the need to update all of the files in a folder should any of the folder names in the path to them change.
Dhanush KPosted Mar 2, 2022, 5:28 PM
Any update on C# 11?
Bohdan StupakPosted Feb 17, 2022, 12:02 PM
Seems like this time Microsoft abandoned the idea to drag more F# features in C#. Anyways nice article!
Mahesh ChandPosted Feb 11, 2022, 7:12 PM
Thank you, awesome!
Mayur GujrathiPosted Feb 11, 2022, 7:57 AM
Thank you Prasad