Aliases with global using directives

Create an alias with C#10 global usings

Home DailyDrop

Daily Knowledge Drop

With the introduction of global usings in C#10, it is now also possible to defined a global alias to have a shortcut to a specific type, across an entire project.


Global usings

A quick intro to global usings - In C#10 the concept of global usings was introduced. It allows for a using statement to be prefixed with global, which then includes that using in all files automatically when compiled.

Assume we have a file called GlobalUsings.cs:

global using System.Threading.Tasks;

In the Program.cs:

// No using System.Threading.Tasks here
Console.WriteLine("About to delay");
Task.Delay(1000);
Console.WriteLine("Delay finished");

Here we can include all includes needed for the project in one file (GlobalUsings.cs), and keep all other files free of usings.


Global alias

This global using functionality can be leverage to create global aliases.

Assume we are writing an application for a library - we need to incorporate the Dewey Decimal System through-out the application. The Dewey Decimal System is basically used to links book(s) to a specific shelf in the library, so they can be easily found - so we decide to use a Dictionary<string,List<string>> to represent this.
The dictionary key will be the shelf reference and the dictionary value will be the list of book titles on specific shelf

Note: There are a number of ways to represent the Dewey Decimal System data, and a Dictionary<string,List<string>> is not necessarily the best or most performant, it is just used for demo purposes.

Throughout code, if we wanted to present this, we could do the following:

var hds = new Dictionary<string,List<string>>();
// OR
Dictionary<string, List<string>> hds1 = new Dictionary<string, List<string>>();

This is a relatively long type to type out the entire time and to clutter up the code - an alternative is to create a global alias for this type.

In the GlobalUsings.cs file we create an alias called dewey and alias it to System.Collections.Generic.Dictionary<string, System.Collections.Generic.List<string>>

global using dewey = System.Collections.Generic.Dictionary<string, 
    System.Collections.Generic.List<string>>;

Now in our application, in any place we want to use System.Collections.Generic.Dictionary<string, System.Collections.Generic.List<string>> we can instead use the alias. Its defined as global so no additional usings are required:

var hds = new dewey();

We can now operate on the dewey type as we would a System.Collections.Generic.Dictionary<string, System.Collections.Generic.List<string>>.

The alias dewey is pointing to the type System.Collections.Generic.Dictionary<string, System.Collections.Generic.List<string>> they are the same type, just with different names.


Conclusion

The concept of an alias is not new to C#, however the new ability to make it global to the entire project makes it a very convenient tool to create shortcuts, especially for the longer named types.


References

Global Using Directives

Daily Drop 11: 15-02-2022

At the start of 2022 I set myself the goal of learning one new coding related piece of knowledge a day.
It could be anything - some.NET / C# functionality I wasn't aware of, a design practice, a cool new coding technique, or just something I find interesting. It could be something I knew at one point but had forgotten, or something completely new, which I may or may never actually use.

The Daily Drop is a record of these pieces of knowledge - writing about and summarizing them helps re-enforce the information for myself, as well as potentially helps others learn something new as well.
c# .net global using directive