You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/csharp/language-reference/keywords/using-directive.md
+16-16Lines changed: 16 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ f1_keywords:
7
7
helpviewer_keywords:
8
8
- "using directive [C#]"
9
9
---
10
-
# using directive
10
+
# The `using` directive
11
11
12
12
The `using` directive allows you to use types defined in a namespace without specifying the fully qualified namespace of that type. In its basic form, the `using` directive imports all the types from a single namespace, as shown in the following example:
13
13
@@ -35,7 +35,7 @@ You can use the `global` modifier on a *using alias directive*.
35
35
36
36
The scope of a `using` directive without the `global` modifier is the file in which it appears.
37
37
38
-
The `global using` directive must appear before all namespace and type declarations. All global using directives must appear in a source file before any non-global`using` directives.
38
+
The `global using` directive must appear before all namespace and type declarations. All global using directives must appear in a source file before any nonglobal`using` directives.
39
39
40
40
Other `using` directives can appear:
41
41
@@ -46,36 +46,36 @@ Otherwise, a compiler error is generated.
46
46
47
47
Create a `using` directive to use the types in a namespace without having to specify the namespace. A `using` directive doesn't give you access to any namespaces that are nested in the namespace you specify. Namespaces come in two categories: user-defined and system-defined. User-defined namespaces are namespaces defined in your code. For a list of the system-defined namespaces, see [.NET API Browser](../../../../api/index.md).
48
48
49
-
## global modifier
49
+
## The `global` modifier
50
50
51
51
Adding the `global` modifier to a `using` directive means that using is applied to all files in the compilation (typically a project). The `global using` directive was added in C# 10. Its syntax is:
52
52
53
53
```csharp
54
54
globalusing <fully-qualified-namespace>;
55
55
```
56
56
57
-
where*fully-qualified-namespace* is the fully qualified name of the namespace whose types can be referenced without specifying the namespace.
57
+
Where*fully-qualified-namespace* is the fully qualified name of the namespace whose types can be referenced without specifying the namespace.
58
58
59
59
A *global using* directive can appear at the beginning of any source code file. All `global using` directives in a single file must appear before:
60
60
61
61
- All `using` directives without the `global` modifier.
62
62
- All namespace and type declarations in the file.
63
63
64
-
You may add `global using` directives to any source file. Typically, you'll want to keep them in a single location. The order of `global using` directives doesn't matter, either in a single file, or between files.
64
+
You can add `global using` directives to any source file. Typically, you want to keep them in a single location. The order of `global using` directives doesn't matter, either in a single file, or between files.
65
65
66
-
The `global` modifier may be combined with the `static` modifier. The `global` modifier may be applied to a *using alias directive*. In both cases, the directive's scope is all files in the current compilation. The following example enables using all the methods declared in the <xref:System.Math?displayProperty=fullName> in all files in your project:
66
+
The `global` modifier can be combined with the `static` modifier. The `global` modifier can be applied to a *using alias directive*. In both cases, the directive's scope is all files in the current compilation. The following example enables using all the methods declared in the <xref:System.Math?displayProperty=fullName> in all files in your project:
67
67
68
68
```csharp
69
69
globalusingstaticSystem.Math;
70
70
```
71
71
72
72
You can also globally include a namespace by adding a `<Using>` item to your project file, for example, `<Using Include="My.Awesome.Namespace" />`. For more information, see [`<Using>` item](../../../core/project-sdk/msbuild-props.md#using).
73
73
74
-
Analyzers issue diagnostics if you've duplicated`global` using directives in different locations. These same analyzers also inform you if you add a `using` directive for a namespace or type that is already referenced by a `global` using directive. You might find it easier to manage your `global` usings by keeping them together in one file in the project.
74
+
Analyzers issue diagnostics if you duplicate`global` using directives in different locations. These same analyzers also inform you if you add a `using` directive for a namespace or type that a `global` using directive already references. You might find it easier to manage your `global` usings by keeping them together in one file in the project.
`using static` imports only accessible static members and nested types declared in the specified type. Inherited members aren't imported. You can import from any named type with a `using static` directive, including Visual Basic modules. If F# top-level functions appear in metadata as static members of a named type whose name is a valid C# identifier, then the F# functions can be imported.
112
+
`using static` imports only accessible static members and nested types declared in the specified type. Inherited members aren't imported. You can import from any named type with a `using static` directive, including Visual Basic modules. If F# top-level functions appear in metadata as static members of a named type whose name is a valid C# identifier, then the F# functions can be imported.
113
113
114
-
`using static` makes extension methods declared in the specified type available for extension method lookup. However, the names of the extension methods aren't imported into scope for unqualified reference in code.
114
+
`using static` makes extension methods declared in the specified type available for extension method lookup. However, the names of the extension methods aren't imported into scope for unqualified reference in code.
115
115
116
-
Methods with the same name imported from different types by different `using static` directives in the same compilation unit or namespace form a method group. Overload resolution within these method groups follows normal C# rules.
116
+
Methods with the same name imported from different types by different `using static` directives in the same compilation unit or namespace form a method group. Overload resolution within these method groups follows normal C# rules.
117
117
118
118
The following example uses the `using static` directive to make the static members of the <xref:System.Console>, <xref:System.Math>, and <xref:System.String> classes available without having to specify their type name.
In the example, the `using static` directive could also have been applied to the <xref:System.Double> type. Adding that directive would make it possible to call the <xref:System.Double.TryParse(System.String,System.Double@)> method without specifying a type name. However, using `TryParse` without a type name creates less readable code, since it becomes necessary to check the `using static` directives to determine which numeric type's `TryParse` method is called.
122
+
In the example, the `using static` directive could also be applied to the <xref:System.Double> type. Adding that directive would make it possible to call the <xref:System.Double.TryParse(System.String,System.Double@)> method without specifying a type name. However, using `TryParse` without a type name creates less readable code, since it becomes necessary to check the `using static` directives to determine which numeric type's `TryParse` method is called.
123
123
124
-
`using static` also applies to `enum` types. By adding `using static` with the enum, the type is no longer required to use the enum members.
124
+
`using static` also applies to `enum` types. By adding `using static` with the enum, the type is no longer required to use the enum members.
125
125
126
126
```csharp
127
127
usingstaticColor;
@@ -142,7 +142,7 @@ class Program
142
142
}
143
143
```
144
144
145
-
## using alias
145
+
## The `using` alias
146
146
147
147
Create a `using` alias directive to make it easier to qualify an identifier to a namespace or type. In any `using` directive, the fully qualified namespace or type must be used regardless of the `using` directives that come before it. No `using` alias can be used in the declaration of a `using` directive. For example, the following example generates a compiler error:
148
148
@@ -165,9 +165,9 @@ Beginning with C# 12, you can create aliases for types that were previously rest
165
165
166
166
## Qualified alias member
167
167
168
-
The namespace alias qualifier, `::` provides explicit access to the global namespace or other using aliases that are potentially hidden by other entities.
168
+
The namespace alias qualifier, `::` provides explicit access to the global namespace or other using aliases potentially hidden by other entities.
169
169
170
-
The `global::` ensures that the namespace lookup for the namespace to the right of the `::` token is relative to the global namespace. Otherwise, the token must resolve to a using alias, and the token to the right must resolve to a type in that aliased namespace. The following example shows both forms:
170
+
The `global::` ensures that the namespace lookup for the namespace following the `::` token is relative to the global namespace. Otherwise, the token must resolve to a using alias, and the token following the `::` must resolve to a type in that aliased namespace. The following example shows both forms:
0 commit comments