Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 5 additions & 3 deletions docs/core/project-sdk/msbuild-props.md
Original file line number Diff line number Diff line change
Expand Up @@ -813,16 +813,18 @@ The `DisableImplicitNamespaceImports` property can be used to disable [implicit

### ImplicitUsings

The `ImplicitUsings` property can be used to enable and disable implicit `global using` directives in C# projects that target .NET 6 or a later version and C# 10.0 or a later version. When the feature is enabled, the .NET SDK adds `global using` directives for a set of default namespaces based on the type of project SDK. Set this property to `true` or `enable` to enable implicit `global using` directives. To disable implicit `global using` directives, remove the property or set it to `false` .
The `ImplicitUsings` property can be used to enable and disable implicit `global using` directives in C# projects that target .NET 6 or a later version and C# 10.0 or a later version. When the feature is enabled, the .NET SDK adds `global using` directives for a set of default namespaces based on the type of project SDK. Set this property to `true` or `enable` to enable implicit `global using` directives. To disable implicit `global using` directives, remove the property or set it to `false` or `disable`.

```xml
<PropertyGroup>
<ImplicitUsings>true</ImplicitUsings>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
```

> [!NOTE]
> For new C# projects that target .NET 6 or later, `ImplicitUsings` is set to `true` by default.
> The templates for new C# projects that target .NET 6 or later have `ImplicitUsings` set to `enable` by default.

To define an explicit `global using` directive, add a [Using](#using) item.

## Items

Expand Down
30 changes: 24 additions & 6 deletions docs/core/tutorials/top-level-templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,38 @@ The features that make the new program simpler are *top-level statements*, *glob

[Top-level statements](../../csharp/fundamentals/program-structure/top-level-statements.md) means the compiler generates the namespace, class, and method elements for your main program. You can look at the code for the new application and imagine that it contains the statements inside the `Main` method generated by earlier templates. You can add more statements to the program, just like you can add more statements to your `Main` method in the traditional style. You can even add functions. They're created as local functions nested inside the generated `Main` method.

*Global `using` directives* means the compiler automatically imports a set of [`using` directives](../../csharp/language-reference/keywords/using-directive.md) based on the project type. For console applications, the following directives are implicitly included in every source file in the application:
Both top-level statements and [implicit `using` directives](#implicit-using-directives) simplify the code that makes up your application. To follow an existing tutorial, add any new statements to the *Program.cs* file generated by the template. You can imagine that the statements you write are between the open and closing braces in the `Main` method in the instructions of the tutorial.

If you'd prefer to use the older format, you can copy the code from the second example in this article, and continue the tutorial as before.

You can learn more about top-level statements in the tutorial exploration on [top-level statements](../../csharp/whats-new/tutorials/top-level-statements.md).

## Implicit `using` directives

*Implicit `using` directives* mean the compiler automatically adds a set of [`using` directives](../../csharp/language-reference/keywords/using-directive.md) based on the project type. For console applications, the following directives are implicitly included in the application:

- `using System;`
- `using System.IO;`
- `using System.Collections.Generic;`
- `using System.Linq;`
- `using System.Net.Http;`
- `using System.Net.Http.Json;`
- `using System.Threading;`
- `using System.Threading.Tasks;`

Other application types include more namespaces that are common for those application types.

These two features simplify the code that makes up your application. To follow an existing tutorial, add any new statements to the *Program.cs* file generated by the template. You can imagine that the statements you write are between the open and closing braces in the `Main` method in the instructions of the tutorial.
### Disable implicit `using` statements

If you'd prefer to use the older format, you can copy the code from the second example in this article, and continue the tutorial as before.
If you want to remove this behavior and manually control all namespaces in your project, add [`<ImplicitUsings>disable</ImplicitUsings>`](../project-sdk/msbuild-props.md#implicitusings) in the project file.

## Global `using` directives

You can learn more about top-level statements in the tutorial exploration on [top level statements](../../csharp/whats-new/tutorials/top-level-statements.md).
A *global `using` directive* imports a namespace for your whole application instead of a single file. These global directives can be added either by adding a [`<Using>`](../project-sdk/msbuild-props.md#using) item to the project file, or by adding the `global using` directive to a code file.

If you want to opt-out of that behavior and manually control all namespaces in your project, add [`<DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports>`](../project-sdk/msbuild-props.md#disableimplicitnamespaceimports) in the project file.
You can also add a [`<Using>`](../project-sdk/msbuild-props.md#using) item in your project file to remove a specific [implicit `using` directive](#implicit-using-directives). For example, if the implicit usings feature is turned on with [`<ImplicitUsings>enable</ImplicitUsings>`](../project-sdk/msbuild-props.md#implicitusings), adding the following `<Using>` item removes the `System.Net.Http` namespace from those that are implicitly imported:

```xml
<ItemGroup>
<Using Remove="System.Net.Http" />
</ItemGroup>
```