Introduction
In this article, we are going to discuss new features introduced in C# 10. This is the first article of the series, and we will cover,
- What is “using” Directive?
- Global modifier with using Directive.
This article can be used by beginners, intermediates, and professionals.
C# 10 was released in Nov 2021.
Prerequisite
We need below prerequisite to work with C#10.
- .Net 6 and newer version
- Visual Studio 2022 or Visual studio code.
If we want to learn how to Setup Visual Studio Code for C# 10 and .Net 6.0, Click here
What is Using Directive?
A question before we start,
How will we use all types available in the namespace? The answer would be import namespace in the class. Import namespace can be done by adding a “using” statement at the top of the class like below example.
using System;
namespace SampleNamespace
{
public class SampleClass{
}
}
We have imported the “System” namespace in our test class.
“using” directive Helps us to use all types available in the namespace. It will not give you access to any namespace nested in that specific namespace.
Below two modifiers can be applied to “using” directive
- Global modifier – Introduced in C# 10.
- Static modifier – Introduced in C# 6 – We will discuss this in the next article.
Global using directive
Suppose we have many classes, and all classes need to use the “System.Collections.Generic” namespace, how will you achieve it?
The answer would be before C# 10 – Import “System.Collections.Generic” namespace in all the classes.
Developers hate to import the same namespaces in multiple classes and that’s why Microsoft added a global modifier in C# 10.
Adding a global modifier to the “using” directive allows you to use all the types of the namespace throughout the project files.
Syntax would be
global using <fully-qualified-namespace>;
in the above syntax,
fully-qualified-namespace - the fully qualified name of the namespace whose types can be referenced without specifying the namespace.
A global using directive should be in the beginner of the code. It means it should be declared before
- Any using directive without a global modifier
- Any other namespace or any types
The best way to use the “global using” directive is in a single separate file. Recommended file name would be “using. cs” or “imports. cs”. The order of global using directive doesn’t matter, it can be in any sequence.




Join the conversation! Your thoughts help the community grow.