Skip to main content

How to Create a using Alias in C#

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Two namespaces sometimes contain a type with the same name, and C# can't tell which one you mean. A using alias settles it by giving one of them a name for your file, as in using Timer = System.Threading.Timer;. It can also shorten a long type name.

Syntax​

Syntax
using <Alias> = <NamespaceOrType>;
PartWhat it does
<Alias>The short name you'll use in this file. It may even be the type's own name, such as Timer.
<NamespaceOrType>The full name it stands for, such as System.Threading.Timer or System.Collections.Generic.List<string>.

Write aliases at the top of the file, with the other using directives. Microsoft's using directive reference: "Create a using alias directive to make it easier to qualify an identifier to a namespace or type."

The Example​

The file imports System.Timers, which has a Timer class, while System.Threading, which has another Timer, is imported automatically. An alias decides which Timer the file means. A second alias, Names, shortens List<string>.

Prerequisites​

  • The .NET 10 SDK or newer, which runs a single .cs file with dotnet run. This guide uses .NET SDK 10.0.404.

Step 1: Write the Code​

Create a file named main.cs in an empty folder:

main.cs, which imports System.Timers, aliases Timer to System.Threading.Timer and Names to a List of string, prints the full name of Timer, and creates a Names list of Leo and Maya and prints its count.

main.cs
using System.Timers;
using Timer = System.Threading.Timer;
using Names = System.Collections.Generic.List<string>;

Console.WriteLine(typeof(Timer).FullName);

Names names = ["Leo", "Maya"];
Console.WriteLine(names.Count);

typeof(Timer).FullName prints the full name of the type that Timer refers to.

Step 2: Run It​

Terminal
dotnet run main.cs
Output
System.Threading.Timer
2

A zsh terminal where dotnet run main.cs prints System.Threading.Timer and 2.

Timer meant System.Threading.Timer, as the alias said, and Names worked exactly like List<string>. The command is the same in zsh, bash and PowerShell.

The Error an Alias Fixes​

Without the alias, the same file has two Timer types in scope:

main.cs
using System.Timers;

Console.WriteLine(typeof(Timer).FullName);
Output
/Users/you/demo/main.cs(3,26): error CS0104: 'Timer' is an ambiguous reference between 'System.Timers.Timer' and 'System.Threading.Timer'

The build failed. Fix the build errors and run again.

System.Threading came from the implicit using directives that single-file apps and new projects get automatically. Microsoft's using directive errors reference describes CS0104: "The compiler can't determine which type you intend to use when the same name appears in two or more namespaces imported by using directives". Its fix: "Use a fully qualified name or a namespace alias when your code references a name that exists in multiple imported namespaces."

Common Mistakes​

MistakeWhat happensDo this instead
Importing two namespaces with the same type name and using the bare nameerror CS0104: 'Timer' is an ambiguous reference between 'System.Timers.Timer' and 'System.Threading.Timer'Add using Timer = <the one you want>;
Aliasing an open generic type, such as List<T>Not allowedAlias a closed type, such as List<string>
Expecting the alias to work in other filesAn alias applies only to the file it's written inUse global using for an alias that every file needs

Troubleshooting​

SymptomLikely causeFix
CS0104Two imported namespaces have a type with that nameAlias the one you mean, or write its full name
The wrong type is usedThe alias points to a different type than you thoughtPrint typeof(<Alias>).FullName

FAQ​

Can an alias stand for a generic type?​

For a closed one, yes. The reference: "A using alias directive can't have an open generic type on the right-hand side. For example, you can't create a using alias for a List<T>, but you can create one for a List<int>."

Which types can I alias?​

Since C# 12, almost any. The reference: "You can create aliases for tuple types, pointer types, and other unsafe types." So using Point = (int X, int Y); is allowed too.

Conclusion​

using <Alias> = <FullName>; gives a namespace or type a name for one file. Use it to settle CS0104 name clashes, such as the two Timer classes, and to shorten long type names like List<string>.

Additional Resources​