59

How can I disable the new default global usings in C# 10 (see here: https://github.com/dotnet/aspnetcore/issues/32451)?

I want to see the used namespaces at a glance and don't want to look up the documentation which namespaces are used globally.

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
Martinaut
  • 1,868
  • 1
  • 17
  • 27

3 Answers3

92

<ImplicitUsings>disable</ImplicitUsings> has to be added to the PropertyGroup in the csproj-file.

For example:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>net6.0</TargetFramework>
        <Nullable>enable</Nullable>
        <ImplicitUsings>disable</ImplicitUsings>
    </PropertyGroup>
</Project>

If you are using a beta version of .NET 6, you will have to use <DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports>.

somethingRandom
  • 811
  • 11
  • 16
Martinaut
  • 1,868
  • 1
  • 17
  • 27
  • 1
    This applies only for .NET 6 beta-versions. For .NET 6 release candidate 1 or newer, set ImplicitUsings to disable. – Martinaut Oct 19 '21 at 12:00
  • Thanks you, it also applies for the released .NET 6.0 LTS version. – Stefan Dec 01 '21 at 14:27
  • Removing `enable` from the project file did not fix the issue but setting it to `disabled` did. My project is multi-targeted: `net6.0;net5.0` – Daniel Gabriel Feb 18 '22 at 23:02
13

In my case I had to remove

<ImplicitUsings>enable</ImplicitUsings>

from csproj

Luca Trazzi
  • 1,240
  • 1
  • 13
  • 30
6

You cannot.

The question asks how to disable C# 10's global using directive feature, whereby prefixing a using directive with global applies the directive to the entire compilation unit (usually the project).

The presently accepted answer of disabling the ImplicitUsings MSBuild property does not affect the global using feature. Instead, "implicit usings" is a separate feature that builds upon the global using feature by automatically, transparently injecting a set of global using directives into the compilation unit, the specific assemblies referenced depending on the project type. Disabling the ImplicitUsings property prevents this injection but leaves the global using directive fully functional.

mkjeldsen
  • 2,053
  • 3
  • 23
  • 28