HOWTO · Csharp

How to Define the Type Alias in C#

Learn how C# using aliases rename types and namespaces, and when inheritance or a wrapper type is a better fit.

On this page

A C# type alias gives an existing type another name. It does not declare a new type, change the type’s behavior, or create a separate type identity. For example, a variable declared through an alias is still the aliased type, so the alias is useful for readability and resolving name ambiguity rather than for adding type safety.

The built-in mechanism is a using alias directive. Use it when a type name is long or when two imported types have the same name. The alias is available in the source scope where the directive is declared; it is not automatically shared across projects. If a distinct type with its own behavior or identity is needed, inheritance or a wrapper may be more appropriate.

What a C# type alias does

An alias is a source-level name for a type or namespace. It does not wrap values, create a subclass, or make otherwise inaccessible members accessible. The compiler resolves the alias to its target type, so APIs that accept that target type also accept values named through the alias.

This distinction matters when choosing a design. Use an alias to shorten references or make the target clear. Use a separately declared type when the program needs a distinct concept that can have its own behavior, validation, or API contract.

Create a type alias with a using directive

Put a using alias directive before the declarations that use it, such as using Writer = System.IO.StringWriter;. Then use Writer in place of the full type name. The directive applies in its declared compilation-unit or namespace scope; it does not create a project-wide name. A local alias can also make two otherwise ambiguous type names explicit without importing every name from their namespaces.

Run this example:

using System;
using Writer = System.IO.StringWriter;

class Program
{
    static void Main()
    {
        using (Writer writer = new Writer())
        {
            writer.Write("Alias target: ");
            writer.Write(typeof(Writer).Name);
            Console.WriteLine(writer.ToString());
        }
    }
}

The alias is only a shorter spelling: typeof(Writer) refers to System.IO.StringWriter. The using keyword also appears in resource-management statements such as the using block above; that separate construct disposes the writer when control leaves the block.

Alias a namespace with a using directive

A namespace alias uses the same form, with a namespace as the target: using Drawing = System.Drawing;. Code can then refer to a type as Drawing.Point where that namespace and type are available. The namespace keyword declares a namespace; by itself it does not create an alias.

A normal namespace import such as using System; makes names in that namespace available for lookup, but it does not automatically import nested namespaces. A namespace alias only shortens a name. It does not change access rules or grant access to private members.

Use inheritance when you need a distinct type

Inheritance declares a new type based on an existing type. For example, class NamedList : System.Collections.Generic.List<string> { } declares NamedList, a distinct class that can be used as a list of strings. Unlike an alias, it has its own declared type identity and can be extended with members or other behavior.

Choose inheritance only when the derived type is a meaningful subtype and the base type permits derivation. Inheritance cannot derive from a sealed class or a value type such as int. It also introduces a relationship to the base class that an alias does not. For simple shorthand, use an alias; for a distinct domain concept, consider a derived type or a wrapper designed around the behavior the concept needs.

Evaluate third-party packages for wrapper types

A third-party package may provide helper abstractions for wrapping or naming types, but that is a package feature, not a built-in C# alias. Evaluate its documentation, maintenance, compatibility, license, and API before adding it. Check whether it creates a distinct wrapper type, what conversions or operations it supports, and whether those semantics fit the code.

A package adds a dependency that users of the project may need to restore and maintain. Avoid adding one just to shorten a type name: a using alias already handles that task without changing type identity. Consider a package only when its verified capabilities solve a broader design need that a simple alias cannot address.