.NET 6: Global Using Directives

.NET 6: Global Using Directives

A long and extensive list of using directives at the top of a .cs or .razor file is a familiar sight as a C# developer. The more dependencies you have, the longer this list goes. Some apply the #region directive on this part of the code, while others utilize tinker with their IDE setting— just to be able to collapse this area.

This convention has been around since .NET Framework 1.0 was launched in the year 2002. Although there is nothing technically wrong with this, it clutters the top space of the files. Additionally, many common libraries are duplicated across multiple files even under same namespaces.

As of November 2021, developers now have an option to address this issue through C# 10's global using directive.

Global Using Directive

With this new feature, a developer may add the global modifier to a using directive, to signify that the qualified namespace is applied to all files during compilation.

Syntax

global using <namespace>;

The global modifier may also be combined with the static modifier or applied to a using alias directive.

global using static System.Math;

Some rules to keep track of:

  • global using directives must appear before all using directives without the global modifier
  • global using directives must appear before all namespace and type declarations in the file

Moreover, the order of global using directives does not matter, either in a single file, or between multiple files. The global using directives may be placed in any source file, but personally, I like to keep them all in one file since it's more organized in that manner.

Implicit Global Using Directives

.NET 6 also offers a set of implicit global using directives for projects for the most common libraries. This means that the compiler will automatically add a bunch of namespaces based on the project type, which will then be available out of the box.

As an example, the following libraries are implicitly added in a console application, which we do not have to specify them.

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

You may deactivate the implicit global using directives by adding this code to the .csproj file.

<ImplicitUsings>disable</ImplicitUsings>

Requirements

To be able to use the global using directives, you must have the following:

Reflection

This newly added feature is helpful in maintaining the cleanliness of our source files. However, before we use the global using directives in different libraries, we must first consider and assess the situation.

  • Will it introduce any naming conflicts in the project? If so, will aliasing the namespace be an option?
  • Will the directive be used in one file? If so, there is no need to add it in the global scope.

If you would like to learn more about .NET 6 and C# 10, please check the following references.