C# Reference ยท Keywords and Statements
using directives
Reference for C# using directives: importing namespaces, using static, alias directives, global using, implicit usings and resolving name conflicts.
A using directive at the top of a file (or inside a namespace) lets you refer to types without writing their full namespace. It does not load an assembly and has no run-time effect: it only affects how names are resolved at compile time. There are four variants: a plain namespace import, using static for the static members of a type, an alias, and global using, which applies to every file in the compilation.
Directives must appear before any type or namespace declarations in their scope. File-scoped namespaces (namespace App;) can be followed by more using lines, and so can block namespaces.
using System.Text; using static System.Math; using Json = System.Text.Json.JsonSerializer; global using System.Linq;
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
using Namespace; using static Namespace.TypeName; using Alias = Namespace.TypeName; using Alias = (int X, int Y); // any type, including tuples, arrays, pointers (C# 12) global using Namespace; global using static Namespace.TypeName; global using Alias = Namespace.TypeName;
| Name | Description |
|---|---|
Namespace |
A namespace whose public types become available by simple name. Nested namespaces are not imported automatically. |
static TypeName |
Brings the type's static members, nested types and (with using static) extension methods into scope. |
Alias |
A new name for a namespace or type. |
Errors: an unknown namespace gives CS0246 or CS0234 ("The type or namespace name ... does not exist"). A name found in two imported namespaces is ambiguous: CS0104. A using after a type declaration is CS1529. Unused imports produce only an IDE hint, not a compiler error.
Usage notes
- Global usings declared in any file apply to the whole project. SDK projects with
ImplicitUsingsenabled addSystem,System.Collections.Generic,System.IO,System.Linq,System.Net.Http,System.ThreadingandSystem.Threading.Tasksthis way (web projects add more). The runner on this site does the same. - Order: conventionally
Systemnamespaces first, then others alphabetically; the order never changes meaning. - Conflicts: use an alias (
using Timer = System.Timers.Timer;) or the fully qualified name. Theglobal::qualifier (see global) starts the lookup at the root. using staticis handy forMath,Consoleand enums, but too many of them hide where a name comes from.- Extension methods become available only when their namespace is imported, which is why
.Where(...)needsSystem.Linq. - Scope: a directive placed inside a namespace applies only to that namespace; placed at the top of a file it applies to the whole file.
- Different from the statement:
using (resource)andusing var x = ...;dispose objects (see using statement and declaration).