Skip to content

Commit b374b8b

Browse files
authored
Clarify using directive examples (#43580)
1 parent a829f87 commit b374b8b

File tree

9 files changed

+63
-28
lines changed

9 files changed

+63
-28
lines changed

docs/core/compatibility/sdk/8.0/implicit-global-using-netfx.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Default projects should compile.
3131

3232
If you relied on the implicit global `using` directive, you can:
3333

34-
- Add a [global `using` directive](../../../../csharp/language-reference/keywords/using-directive.md#global-modifier) to one of your source files.
34+
- Add a [global `using` directive](../../../../csharp/language-reference/keywords/using-directive.md#the-global-modifier) to one of your source files.
3535
- Add a `using` directive to each source code file that uses APIs from System.Net.Http.
3636

3737
## Affected APIs

docs/core/project-sdk/msbuild-props.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1751,7 +1751,7 @@ For more information, see [Trimming options](../deploying/trimming/trimming-opti
17511751

17521752
### Using
17531753

1754-
The `Using` item lets you [globally include a namespace](../../csharp/language-reference/keywords/using-directive.md#global-modifier) across your C# project, such that you don't have to add a `using` directive for the namespace at the top of your source files. This item is similar to the `Import` item that can be used for the same purpose in Visual Basic projects. This property is available starting in .NET 6.
1754+
The `Using` item lets you [globally include a namespace](../../csharp/language-reference/keywords/using-directive.md#the-global-modifier) across your C# project, such that you don't have to add a `using` directive for the namespace at the top of your source files. This item is similar to the `Import` item that can be used for the same purpose in Visual Basic projects. This property is available starting in .NET 6.
17551755

17561756
```xml
17571757
<ItemGroup>
@@ -1772,7 +1772,7 @@ For example:
17721772
- `<Using Include="Microsoft.AspNetCore.Http.Results" Alias="Results" />` emits `global using Results = global::Microsoft.AspNetCore.Http.Results;`
17731773
- `<Using Include="Microsoft.AspNetCore.Http.Results" Static="True" />` emits `global using static global::Microsoft.AspNetCore.Http.Results;`
17741774

1775-
For more information, see [aliased `using` directives](../../csharp/language-reference/keywords/using-directive.md#using-alias) and [`using static <type>` directives](../../csharp/language-reference/keywords/using-directive.md#static-modifier).
1775+
For more information, see [aliased `using` directives](../../csharp/language-reference/keywords/using-directive.md#the-using-alias) and [`using static <type>` directives](../../csharp/language-reference/keywords/using-directive.md#the-static-modifier).
17761776

17771777
## Item metadata
17781778

docs/core/project-sdk/overview.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ If you explicitly define any of these items in your project file, you're likely
116116

117117
## Implicit using directives
118118

119-
Starting in .NET 6, implicit [`global using` directives](../../csharp/language-reference/keywords/using-directive.md#global-modifier) are added to new C# projects. This means that you can use types defined in these namespaces without having to specify their fully qualified name or manually add a `using` directive. The *implicit* aspect refers to the fact that the `global using` directives are added to a generated file in the project's *obj* directory.
119+
Starting in .NET 6, implicit [`global using` directives](../../csharp/language-reference/keywords/using-directive.md#the-global-modifier) are added to new C# projects. This means that you can use types defined in these namespaces without having to specify their fully qualified name or manually add a `using` directive. The *implicit* aspect refers to the fact that the `global using` directives are added to a generated file in the project's *obj* directory.
120120

121121
Implicit `global using` directives are added for projects that use one of the following SDKs:
122122

docs/csharp/language-reference/builtin-types/value-tuples.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ At compile time, the compiler replaces non-default field names with the correspo
6363
> [!TIP]
6464
> Enable .NET code style rule [IDE0037](../../../fundamentals/code-analysis/style-rules/ide0037.md) to set a preference on inferred or explicit tuple field names.
6565
66-
Beginning with C# 12, you can specify an alias for a tuple type with a [`using` directive](../keywords/using-directive.md#using-alias). The following example adds a `global using` alias for a tuple type with two integer values for an allowed `Min` and `Max` value:
66+
Beginning with C# 12, you can specify an alias for a tuple type with a [`using` directive](../keywords/using-directive.md#the-using-alias). The following example adds a `global using` alias for a tuple type with two integer values for an allowed `Min` and `Max` value:
6767

6868
:::code language="csharp" source="snippets/shared/ValueTuples.cs" id="AliasTupleType":::
6969

docs/csharp/language-reference/compiler-messages/using-directive-errors.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ In addition, if you define an alias named `global`, the compiler issues **CS0440
154154

155155
## Alias name conflicts
156156

157-
You can declare an [alias](../keywords/using-directive.md#using-alias) to a namespace or a type with a `using` directive:
157+
You can declare an [alias](../keywords/using-directive.md#the-using-alias) to a namespace or a type with a `using` directive:
158158

159159
:::code language="csharp" source="./snippets/UsingDirectives/Program.cs" id="UsingAlias":::
160160

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
// <UsingAliasQualifier>
3+
using S = System.Net.Sockets;
4+
5+
class A
6+
{
7+
public static int x;
8+
}
9+
10+
class C
11+
{
12+
public void F(int A, object S)
13+
{
14+
// Use global::A.x instead of A.x
15+
global::A.x += A;
16+
17+
// Using ::, S must resolve to a namespace alias:
18+
S::Socket s = S as S::Socket;
19+
20+
// In this form, if S were a class, it would be a compile-time error:
21+
S.Socket s1 = S as S.Socket;
22+
}
23+
}
24+
// </UsingAliasQualifier>

docs/csharp/language-reference/keywords/using-directive.md

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
---
2-
description: "using directive - C# Reference"
3-
title: "using directive"
4-
ms.date: 08/19/2021
2+
description: "The `using` directive imports types from a namespace, or creates an alias for a given type. Using directives enable you to use simple names for types instead of the fully qualified type name."
3+
title: "The using directive: Import types from a namespace"
4+
ms.date: 11/14/2024
55
f1_keywords:
66
- "using_CSharpKeyword"
77
helpviewer_keywords:
88
- "using directive [C#]"
9-
ms.assetid: b42b8e61-5e7e-439c-bb71-370094b44ae8
109
---
11-
# using directive
10+
# The `using` directive
1211

1312
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:
1413

@@ -21,7 +20,7 @@ You can apply two modifiers to a `using` directive:
2120
- The `global` modifier has the same effect as adding the same `using` directive to every source file in your project. This modifier was introduced in C# 10.
2221
- The `static` modifier imports the `static` members and nested types from a single type rather than importing all the types in a namespace.
2322

24-
You can combine both modifiers to import the static members from a type in all source files in your project.
23+
You can combine both modifiers to import the static members from a type to all source files in your project.
2524

2625
You can also create an alias for a namespace or a type with a *using alias directive*.
2726

@@ -36,43 +35,47 @@ You can use the `global` modifier on a *using alias directive*.
3635
3736
The scope of a `using` directive without the `global` modifier is the file in which it appears.
3837

39-
The `using` directive can appear:
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+
40+
Other `using` directives can appear:
4041

4142
- At the beginning of a source code file, before any namespace or type declarations.
42-
- In any namespace, but before any namespaces or types declared in that namespace, unless the `global` modifier is used, in which case the directive must appear before all namespace and type declarations.
43+
- In any blocked-scoped namespace, but before any namespaces or types declared in that namespace.
4344

44-
Otherwise, compiler error [CS1529](../compiler-messages/using-directive-errors.md) is generated.
45+
Otherwise, a compiler error is generated.
4546

4647
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).
4748

48-
## global modifier
49+
## The `global` modifier
4950

5051
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:
5152

5253
```csharp
5354
global using <fully-qualified-namespace>;
5455
```
5556

56-
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.
5758

5859
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:
5960

6061
- All `using` directives without the `global` modifier.
6162
- All namespace and type declarations in the file.
6263

63-
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.
6465

65-
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:
6667

6768
```csharp
6869
global using static System.Math;
6970
```
7071

7172
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).
7273

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.
75+
7376
[!INCLUDE [csharp10-templates](../../../../includes/csharp10-templates.md)]
7477

75-
## static modifier
78+
## The `static` modifier
7679

7780
The `using static` directive names a type whose static members and nested types you can access without specifying a type name. Its syntax is:
7881

@@ -106,19 +109,19 @@ By eliminating the need to explicitly reference the <xref:System.Math> class eac
106109

107110
:::code language="csharp" source="./snippets/using-static2.cs":::
108111

109-
`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.
110113

111-
`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.
112115

113-
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.
114117

115118
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.
116119

117120
:::code language="csharp" source="./snippets/using-static3.cs" id="Snippet1":::
118121

119-
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.
120123

121-
`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.
122125

123126
```csharp
124127
using static Color;
@@ -139,7 +142,7 @@ class Program
139142
}
140143
```
141144

142-
## using alias
145+
## The `using` alias
143146

144147
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:
145148

@@ -160,6 +163,14 @@ The following example shows how to define a `using` directive and a `using` alia
160163

161164
Beginning with C# 12, you can create aliases for types that were previously restricted, including [tuple types](../builtin-types/value-tuples.md#tuple-field-names), pointer types, and other unsafe types. For more information on the updated rules, see the [feature spec](~/_csharplang/proposals/csharp-12.0/using-alias-types.md).
162165

166+
## Qualified alias member
167+
168+
The namespace alias qualifier, `::` provides explicit access to the global namespace or other using aliases potentially hidden by other entities.
169+
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:
171+
172+
:::code language="csharp" source="./snippets/UsingAliasQualifier.cs" id="UsingAliasQualifier":::
173+
163174
## C# language specification
164175

165176
For more information, see [Using directives](~/_csharpstandard/standard/namespaces.md#145-using-directives) in the [C# Language Specification](~/_csharpstandard/standard/README.md). The language specification is the definitive source for C# syntax and usage.

docs/csharp/linq/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ The following example shows a complete query operation. The complete operation i
1414

1515
:::code language="csharp" source="./snippets/linq-index/Index.cs" id="intro":::
1616

17-
You might need to add a [`using`](../language-reference/keywords/using-directive.md) directive, `using System.Linq;`, for the preceding example to compile. The most recent versions of .NET make use of [implicit usings](../../core/project-sdk/overview.md#implicit-using-directives) to add this directive as a [global using](../language-reference/keywords/using-directive.md#global-modifier). Older versions require you to add it in your source.
17+
You might need to add a [`using`](../language-reference/keywords/using-directive.md) directive, `using System.Linq;`, for the preceding example to compile. The most recent versions of .NET make use of [implicit usings](../../core/project-sdk/overview.md#implicit-using-directives) to add this directive as a [global using](../language-reference/keywords/using-directive.md#the-global-modifier). Older versions require you to add it in your source.
1818

1919
## Query expression overview
2020

docs/csharp/tour-of-csharp/overview.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ The preceding example shows one form of a "Hello, World" program, using [top-lev
2525

2626
:::code language="csharp" interactive="try-dotnet" source="./snippets/shared/HelloWorld.cs":::
2727

28-
This version shows the building blocks you use in your programs. The "Hello, World" program starts with a `using` directive that references the `System` namespace. Namespaces provide a hierarchical means of organizing C# programs and libraries. Namespaces contain types and other namespaces—for example, the `System` namespace contains many types, such as the `Console` class referenced in the program, and many other namespaces, such as `IO` and `Collections`. A `using` directive that references a given namespace enables unqualified use of the types that are members of that namespace. Because of the `using` directive, the program can use `Console.WriteLine` as shorthand for `System.Console.WriteLine`. In the earlier example, that namespace was [implicitly](../language-reference/keywords/using-directive.md#global-modifier) included.
28+
This version shows the building blocks you use in your programs. The "Hello, World" program starts with a `using` directive that references the `System` namespace. Namespaces provide a hierarchical means of organizing C# programs and libraries. Namespaces contain types and other namespaces—for example, the `System` namespace contains many types, such as the `Console` class referenced in the program, and many other namespaces, such as `IO` and `Collections`. A `using` directive that references a given namespace enables unqualified use of the types that are members of that namespace. Because of the `using` directive, the program can use `Console.WriteLine` as shorthand for `System.Console.WriteLine`. In the earlier example, that namespace was [implicitly](../language-reference/keywords/using-directive.md#the-global-modifier) included.
2929

3030
The `Hello` class declared by the "Hello, World" program has a single member, the method named `Main`. The `Main` method is declared with the `static` modifier. While instance methods can reference a particular enclosing object instance using the keyword `this`, static methods operate without reference to a particular object. By convention, when there are no top-level statements a static method named `Main` serves as the [entry point](../fundamentals/program-structure/main-command-line.md) of a C# program.
3131

0 commit comments

Comments
 (0)