Skip to content

Commit 5e55c3e

Browse files
shethaaditAdit ShethBillWagner
authored
Enhanced Namespace Documentation: Comprehensive Insights on using in File-Scoped Declarations. (#43304)
* Updated namespace.md to resolve bug 43266. * Apply suggestions from code review --------- Co-authored-by: Adit Sheth <[email protected]> Co-authored-by: Bill Wagner <[email protected]>
1 parent 92e109f commit 5e55c3e

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

docs/csharp/language-reference/keywords/namespace.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,32 @@ The `namespace` keyword is used to declare a scope that contains a set of relate
2020

2121
:::code language="csharp" source="snippets/filescopednamespace.cs" :::
2222

23+
## Using Statements in File Scoped Namespaces
24+
25+
When using *file-scoped namespaces*, the placement of `using` statements affects their scope within the file. File-scoped namespaces lower to the equivalent traditional namespace declaration that ends with a closing bracket at the end of the file. This behavior determines where `using` directives are applied as follows:
26+
27+
- If the `using` statements are placed before the file-scoped namespace declaration, they are treated as being outside of the namespace and are interpreted as fully-qualified namespaces.
28+
- If the `using` statements are placed after the file-scoped namespace declaration, they are scoped within the namespace itself.
29+
30+
For example:
31+
32+
```csharp
33+
// This using is outside the namespace scope, so it applies globally
34+
using System;
35+
36+
namespace SampleNamespace; // File-scoped namespace declaration
37+
38+
// This using is inside the namespace scope
39+
using System.Text;
40+
41+
public class SampleClass
42+
{
43+
// Class members...
44+
}
45+
```
46+
47+
In the above example, `System` is globally accessible, while `System.Text` applies only within `SampleNamespace`.
48+
2349
The preceding example doesn't include a nested namespace. File scoped namespaces can't include additional namespace declarations. You cannot declare a nested namespace or a second file-scoped namespace:
2450

2551
```csharp

0 commit comments

Comments
 (0)