skip to content

In C#, what do using static and using alias directives bring into scope, and which types can a C# 12 using alias name?

level: middleimportance: nice to knowfreq 28%

answer

  1. one type's static members
  2. extension methods stay extensions
  3. an alias is a name, not a type
  4. tuples, arrays and pointers since C# 12

basics

~20 s

using static imports one type's static members and nested types; a using alias gives a namespace or type a second name. Since C# 12 an alias can name any type, such as a tuple or array, but never a nullable reference type.

solid answer

~50 s

`using static System.Math;` lets a file call `Max` or `Sqrt` without the type name. It imports only that type's own accessible static members and nested types, not inherited ones, and it makes extension methods available only as extensions: `xs.Where(...)` works, a bare `Where(xs, ...)` is CS0103. A member with the same name in the type you are writing hides the import entirely. `using Ids = int[];` is an alias: another spelling of an existing type, scoped to the file unless marked `global`. Its target is resolved as if the other using directives at that level did not exist, so it needs the full name. Before C# 12 an alias could name only a namespace or a named type; now tuples, arrays, `int?` and, with `using unsafe`, pointers work too, while `using Label = string?;` is error CS9132 and an open generic is never allowed. The alias creates no new type.

code

csharp · 15 lines
csharp
using Checkpoint = (string Script, int Version);
using Ids = int[];
using MaybeVersion = int?;
using static System.Math;

Checkpoint last = ("0042_add_index.sql", 42);
Ids pending = [43, 44];
MaybeVersion target = null;

Console.WriteLine($"{last.Script} v{last.Version}, next {Max(pending[0], pending[1])}");
Console.WriteLine($"target: {target?.ToString() ?? "latest"}");
Console.WriteLine(typeof(Checkpoint));

(string, int) raw = last; // same type, so no conversion is needed
Console.WriteLine(raw == last);

go deeper

for a junior

Recall that using static drops the type name from static calls and that a using alias gives a type or namespace a short second name.

for a middle

Explain the limits: no inherited or instance members, extension methods only as extensions, alias targets resolved without the neighbouring usings, and what C# 12 allowed aliases to name.

for a senior

Use aliases to fix ambiguities, recognise when a member in scope hides a using static import, and push back when a tuple alias is standing in for a type that needs its own rules.

for a principal

Set a team rule on using static and global aliases: allowed for a few unambiguous helpers and shared tuple shapes, replaced by real types once a shape carries behaviour.

## using static: one type's static members by simple name `using static` (C# 6) names a **type**, not a namespace, and imports that type's accessible **static members** and **nested types** so they can be used without the type name. It works on any type with static members, including enums, whose members are static constants: after `using static System.ConsoleColor;`, `Red` means `ConsoleColor.Red`. The edges interviewers probe: - **Inherited members are not imported**, only members the named type declares. - **Instance members are not imported**; they still need an instance. - **Extension methods** in the type become available for extension-method lookup, but their names are not imported for unqualified calls. With `using static System.Linq.Enumerable;`, `Range(1, 3)` works and `orders.Where(o => o.Paid)` works, but `Where(orders, o => o.Paid)` is **CS0103**, "The name 'Where' does not exist in the current context". - **Members in scope hide the import.** If the class you are writing declares `Max(int, int, int)`, a call `Max(1, 2)` binds to that method alone and fails with **CS7036** (no argument for parameter `c`); `Math.Max` is not added as an extra overload. ## using alias: a second name for a namespace or type `using Sql = System.Data.Common;` or `using Timer = System.Timers.Timer;` introduces an **alias**. Aliases are scoped to the file or namespace body they appear in; `global using` makes one visible to the whole compilation. Three rules catch people: 1. The target is resolved **as if the other using directives at that level did not exist**. At the top of a file that also ignores global and implicit usings, so `using Sb = StringBuilder;` fails with **CS0246** even beside `using System.Text;`. Write `System.Text.StringBuilder`, or place the alias inside a namespace body, where the file-level directives apply as an outer scope. 2. An alias in a file is checked **before** namespace imports, which is why an alias is the standard fix for a CS0104 ambiguity. 3. An alias cannot take type parameters: `using Bag<T> = List<T>;` is a syntax error, and `using Bag = List<T>;` cannot find `T`. A closed type such as `List<int>` is fine. ## C# 12: aliasing any type Before C# 12 the target had to be a namespace or a named type. C# 12 accepts **any type**: | Alias | C# 12 and later | |---|---| | `using Checkpoint = (string Script, int Version);` | allowed; element names travel with the alias | | `using Ids = int[];` | allowed | | `using MaybeVersion = int?;` | allowed: a nullable **value** type | | `using Names = System.Collections.Generic.List<string?>;` | allowed: the alias itself is not a nullable reference type | | `using Label = string?;` | error **CS9132**, "Using alias cannot be a nullable reference type." | | `using Ptr = int*;` | error **CS0214** unless written `using unsafe Ptr = int*;` with unsafe code allowed in the project | Compiling a tuple alias with `LangVersion` 11 gives **CS9058**, "Feature 'using type alias' is not available in C# 11.0". ## Where an alias is visible - An alias at the top of a file is visible in that file only. - An alias written inside a namespace declaration is visible only in that declaration's body, not in other files that declare the same namespace. - A `global using` alias is visible in every file of the compilation, so its name must be unique across the project; like other global imports, a member with the same name in a nearer scope hides it. ## An alias is not a new type `typeof(Checkpoint)` is `System.ValueTuple`2[System.String,System.Int32]`, and any `(string, int)` value converts to and from it freely. The alias improves readability but gives no type safety: a `(string, int)` meaning something else is accepted wherever `Checkpoint` is expected. When the compiler should keep two concepts apart, declare a `record struct Checkpoint(string Script, int Version)` instead. ## Choosing between them in a code review - Use `using static` for a few heavily used helpers (`Math`, a constants class) where the call site stays obvious; avoid it for types like `int`, where a bare `Parse` hides which type is parsing. - Use a file-level alias to settle an ambiguity or shorten one long closed generic. - Use a `global using` alias for a tuple shape many files share, and switch to a real type when that shape starts carrying rules.

  • Why can't `using static System.Linq.Enumerable;` let you call `Where(orders, o => o.Paid)`?
    using static makes a type's extension methods available for extension-method lookup only; their names are not imported for unqualified calls, so `Where(...)` is CS0103. `orders.Where(...)` still works, and ordinary static methods such as `Range` are imported by simple name.
  • Does `using Checkpoint = (string Script, int Version);` create a new type?
    No. It is another spelling of `ValueTuple<string, int>` with element names; `typeof(Checkpoint)` prints the ValueTuple type and any `(string, int)` value is accepted where a `Checkpoint` is expected. When you need a distinct type the compiler protects, declare a `record struct`.
  • Why does `using Sb = StringBuilder;` fail even though `using System.Text;` is on the line above?
    An alias target is resolved as if the other using directives at that level did not exist, and at file level global usings are ignored too. The compiler reports CS0246. Write `System.Text.StringBuilder`, or declare the alias inside a namespace body, where the file-level imports apply.

  • using static imports a type's instance members as well.
  • using static System.Linq.Enumerable lets you call Where(list, predicate) unqualified.
  • An alias such as using Checkpoint = (string, int) creates a distinct type.
  • Aliases can be generic, as in using Bag<T> = List<T>.
  • A using alias can rely on names imported by the using lines next to it.