Skip to content

Commit 12edaf3

Browse files
committed
Clarify using directive examples
Fixes #43136: Using directives can appear in namespace declarations, unless it's a global using. Fixes #29746: Add notes about how to get diagnostics on duplicated global usings, and considering a standard location for all global usings in a project. Fixes #27331: Rework a couple sentences for clarity. Fixes #26469: Add text an examples for the `::` token for qualified namespace aliases.
1 parent 2757501 commit 12edaf3

File tree

2 files changed

+43
-8
lines changed

2 files changed

+43
-8
lines changed
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: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
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
---
1110
# using directive
1211

@@ -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,12 +35,14 @@ 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 non-global `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

@@ -70,6 +71,8 @@ global using static System.Math;
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'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.
75+
7376
[!INCLUDE [csharp10-templates](../../../../includes/csharp10-templates.md)]
7477

7578
## static modifier
@@ -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 that are potentially hidden by other entities.
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:
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.

0 commit comments

Comments
 (0)