The Global Access Modifier (Organize Your C# Code)

rasheedmozaffar

Rasheed K Mozaffar

Posted on February 7, 2023

The Global Access Modifier (Organize Your C# Code)

As you know , in C# , methods , properties and functionality in general is encapsulated in namespaces , and to access them , you need to stick a Using Directive to import the namespace in which resides the functionality you are looking for , but often times , you find yourself adding the same namespace again and again , which is a bit redundant and annoying , so for that , C# 10 introduced a new feature known as Global Using Directives , this feature solved the redundancy of importing a namespace every time you want to use it.
With global directives , you import it once , and then access it wherever you want in your project source code.

Enough for the introduction , let's see it in action.

Global Using Directives !

To make a using statement global , all you have to do is to just precede the using statement with the global access modifier , but make sure all the global using directives are added to the top of the code file , don't put them after normal using statements , make sure the global directives are the first lines in your code file.

You can take this example as a rule to follow:


//THIS IS WRONG
using Microsoft.AspNetCore.Mvc;
global using Microsoft.EntityFrameworkCore;

Enter fullscreen mode Exit fullscreen mode

This previous example is false , and the compiler will complain by showing a compile-time error saying:

A global using directive must precede all non-global using directives (CS8915)

Now if you reverse the order of the previous example:


//THIS IS CORRECT
global using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Mvc;

Enter fullscreen mode Exit fullscreen mode

The compiler will not complain anymore , and now you can access Microsoft.EntityFrameworkCore globally anywhere in your project files , without having to import the namespace every time you wanna use EntityFrameworkCore.

You may be asking , is there a specific file to hold the global using directives , or do you just put them in any C# file ?

The answer is , you can put global using directives in any C# file you want , like program.cs , but , a better practice is to create a separate file in the root directory of your project , and call it GlobalUsings.cs , it should only contain the global using directives that you may find yourself importing to your code files perpetually , you can take this as an example:


global using System.Text;
global using Microsoft.AspNetCore.Mvc;
global using Microsoft.EntityFrameworkCore;
global using System.Encoding;
global using ServerProject.Models;

Enter fullscreen mode Exit fullscreen mode

The content in GlobalUsings.cs.

This content varies depends on your project type , the packages you might be using , and the structure of your project.

Global using directives really assist in keeping your code files more arranged and less cluttered with using statements.

This feature is extremely handy and easy to use , so start using it more often within your projects , your code will look eventually cleaner and less chaotic.

💖 💪 🙅 🚩
rasheedmozaffar
Rasheed K Mozaffar

Posted on February 7, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related