Global Static Using Directive In C# 10

Introduction

In this article, we are going to discuss how to use the "global using" directive with the "static" modifier. 

I assume that you are aware of the “using” and “global using” directives in C# 10. If not, then please read the “global using in C# 10” article first and then resume on this article to get a better understanding. 

We will start with the “static” modifier and then learn how to combine the “global” modifier and “static” modifier with a simple example.

Static Modifier

Using Static Modifier was introduced in C# 6.0. This using static directive helps us to use static member and nested types without specifying type names. 

Syntax of using static directive is

using static <fully-qualified-type-name>;

fully-qualified-type-name - The <fully-qualified-type-name> is the name of the type whose static members and nested types can be referenced without specifying a type name.

Let’s try to understand this using the below use case,

USECASE

Below code is written without “using static” directive

namespace staticdemo
{
    public class staticdemocls
    {
        public void PrintNames()
        {
            Console.WriteLine("Kirtesh Shah");
            Console.WriteLine("Sweta Shah");
            Console.WriteLine("Nitya Shah");
            Console.WriteLine("Dilip Shah");
            Console.WriteLine("Hansa Shah");
        }
        public void PrintMathSample()
        {
            Console.WriteLine(System.Math.Max(500, 50));
            Console.WriteLine(System.Math.Min(500,50));
            Console.WriteLine(System.Math.Round(500.50));)
        }
    }
}

In the above example,

  1. We have created a class with 2 methods
  2. “Print Names() “ Method prints name on the screen.
  3. “PrintMathSample()” method prints some numbers after doing some calculations using the “System.Math” namespace.

You might notice that we have used the “Console.WriteLine()” method from “System” Namespace and “Max”, “Min”, and “Round” methods from “System.Math” namespace are used multiple times in the above code.

Let’s execute this code,

staticdemocls staticdemoclsobj = new staticdemocls();
staticdemoclsobj.PrintNames();
staticdemoclsobj.PrintMathSample();

Output

Static Modifier

Developers hate to write the same type multiple times, it makes your code complex and not easily readable hence Microsoft introduced “using static“ directives in C# 6.0.

Now we will use “using static” directive in the same example,

using static System.Console;
using static System.Math;

namespace staticdemo
{
    public class staticdemocls
    {
        public void PrintNames()
        {
            WriteLine("Kirtesh Shah");
            WriteLine("Sweta Shah");
            WriteLine("Nitya Shah");
            WriteLine("Dilip Shah");
            WriteLine("Hansa Shah");
        }
        
        public void PrintMathSample()
        {
            WriteLine(Max(500, 50));
            WriteLine(Min(500,50));
            WriteLine(Round(500.50));
        }
    }
}
}

We have declared “using static” directive in the above example. You might notice that now we have used

  1. “WriteLine()” instead of “Console.WriteLine()”.
  2. “Max()” instead of “System.Math.Max()”
  3. “Min()” instead of “System.Math.Min()”
  4. “Round()” instead of “System.Math.Round()”

Let’s execute this code and see the output,

Static Modifier

Using global static modifier

The "global" modifier can combine with the "static" modifier. Let’s combine the "global" modifier and the "static" modifier in the above example.

As we all know, developers don’t prefer/like to import the same using static directive in multiple classes and that’s why Microsoft added a global static modifier in C# 10. 

Adding a global static modifier to the “using” directive allows you to use all the static members of the types of namespaces throughout the project files.

Let’s remove below two statements from the “staticdemocls” class and add them in using.cs file.

global using staticdemo;
global using static System.Console;

Adding it in using.cs file,

global using System;
global using System.Collections.Generic;
global using Example;
global using staticdemo;
global using static System.Console;
global using static System.Math;

Let’s run the code again and see the output,

Using global static modifier

That’s all for this article. This is a simple but very important concept for the developer. I hope you enjoyed this article.


Similar Articles