Global Using Directive In C# 10

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,

  1. What is “using” Directive?
  2. 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. 

  1. .Net 6 and newer version
  2. 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

  1. Global modifier – Introduced in C# 10.
  2. 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

  1. Any using directive without a global modifier
  2. 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.

Let’s try to understand this concept using below example.

We will create a new class called “CollectionHelper.cs” in the ”Example” namespace.

using System;
using System.Collections.Generic;

namespace Example
{
 public class Collectionhelper
 {
        public void DirectoryPrint()
        {
            Dictionary<int, string> directoryObj =  new Dictionary<int, string>(5);

            directoryObj.Add(101, "Kirtesh Shah");
            directoryObj.Add(102, "Nitya Shah");
            directoryObj.Add(103, "Sweta Shah");
            directoryObj.Add(104, "Hansa Shah");
            directoryObj.Add(105, "Dilip Shah");

            for (int i = 101; i < (101+directoryObj.Count); i++)
            {
                Console.WriteLine(directoryObj[i]);
            }
        }
    }
} 

This class has a method called “DirectoryPrint()” that prints values set in the directory object on the console screen.

Now in the program.cs file we will create an object of the “CollectionHelper” class and call the “DirectoryPrint()” method. 

Collectionhelper Collectionhelperobj = new Collectionhelper();
Collectionhelperobj.DirectoryPrint();

Console.WriteLine("Hello, World!");

Please note that we have not imported “Example” Namespace in the above code, hence we will get an error message.

Global modifier with using Directive

Now I have imported namespace “Example” and rerun the project.

using Example;

Collectionhelper Collectionhelperobj = new Collectionhelper();
Collectionhelperobj.DirectoryPrint();

Console.WriteLine("Hello, World!");

Now we will execute again,

Global modifier with using Directive

Wow. Working fine. In the above code we have imported namespace in the “project. cs” and “CollectionHelpe.cs” files. Now we will create a separate file called “using. cs” and insert all namespaces in the single file with the “global using” directive.

First, Let's create "using.cs" file and insert all namespaces,

global using System;
global using System.Collections.Generic;
global using Example;

Remove all namespaces from “project. cs” and “collectionhelped.cs” files.

Now run this code again and see the output,

Global modifier with using Directive

Excellent. The above code is working fine with the “global using” directive. 

It means that we can insert all the namespaces in a single file and it will be available throughout the project. the "global using" directive can combine with a static modifier.

In this article, we have learned about using and global suing directive. We will discuss another wonderful feature of C#10 in the next article. Hope you enjoy this article.


Similar Articles