Where are the using statements/directives in .NET 6

C# 10.0 introduces a new feature called global using directive (global using <fully-qualified-namespace>;) which allows to specify namespaces to be implicitly imported in all files in the compilation. .NET 6 RC1 has this feature enabled by default in new project templates (see <ImplicitUsings>enable</ImplicitUsings> property in your .csproj).

For Microsoft.NET.Sdk.Web next namespaces should be implicitly imported (plus the ones from Microsoft.NET.Sdk):

  • System.Net.Http.Json
  • Microsoft.AspNetCore.Builder
  • Microsoft.AspNetCore.Hosting
  • Microsoft.AspNetCore.Http
  • Microsoft.AspNetCore.Routing
  • Microsoft.Extensions.Configuration
  • Microsoft.Extensions.DependencyInjection
  • Microsoft.Extensions.Hosting
  • Microsoft.Extensions.Logging

UPD

To address your questions in comment:

At the moment of writing the generated file containing default imports will be inside the obj folder named something like ProjectName.GlobalUsings.g.cs.

To modify default imports you can add Using element to your .csproj file. Based on exposed attributes it allows several actions including addition and removal:

<ItemGroup>
    <Using Include="SomeFullyQualifiedNamespace"/>
</ItemGroup>

For just addition you can simply prefix your using directive with global modifier in any file (or create a separate one just for this):

global using SomeFullyQualifiedNamespace;

Leave a Comment