Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions docs/csharp/language-reference/keywords/namespace.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,32 @@ The `namespace` keyword is used to declare a scope that contains a set of relate

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

## Using Statements in File Scoped Namespaces

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:

- If the `using` statements are placed before the file-scoped namespace declaration, they are treated as being outside of the namespace and apply globally.
- If the `using` statements are placed after the file-scoped namespace declaration, they are scoped within the namespace itself.

For example:

```csharp
// This using is outside the namespace scope, so it applies globally
using System;

namespace SampleNamespace; // File-scoped namespace declaration

// This using is inside the namespace scope
using System.Text;

public class SampleClass
{
// Class members...
}
```

In the above example, `System` is globally accessible, while `System.Text` applies only within `SampleNamespace`.

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:

```csharp
Expand Down
Loading