简体   繁体   中英

C# 10: Disable Global Using

How can I disable the new default global usings in C# 10 (see here: https://github.com/do.net/as.netcore/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.

<DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports> 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>
        <DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports>
    </PropertyGroup>
</Project>

In my case I had to remove

<ImplicitUsings>enable</ImplicitUsings>

from csproj

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 using s" 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.

I found the using implicit using statements in the project properties in the Application section.

I do not like implicit using statements or the new nullable features. For me, they make the code harder to read and therefor require more testing, I do not end up with more stable code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM