Update implicit global usings feature to address issues
See original GitHub issueThe new implicit global usings feature added to the SDK in 6.0.100-preview.7 has caused a few issues in some important scenarios such that some changes will be made to mitigate them and generally improve its usability.
These changes are targeting 6.0.100-rc.1
Items:
- Rename the MSBuild property from
<DisableImplicitNamespaceImports>
to<ImplicitUsings>
- Note this is for C# only
- Visual Basic will continue to use the
<DisableImplicitNamespaceImports>
property for disabling implicit namespace imports
- Add the
<ImplicitUsings>
property to the schema file so it appears in statement completion - Change the defined conditions to only enable adding the implicit usings from the SDK when the
<ImplictUsings>
property is set to “true” or “enable”- The existing conditions based on TFM should be removed
- The only condition for adding the implicit usings as defined in the SDK will be the check that
<ImplicitUsings>
is equal to “true” or “enable”
- Rename the item type that specifies the global namespaces to emit to the generated .cs file from
<Import>
to<Using>
- Note this is for C# only
- Visual Basic will continue to use the
<Import>
item type for specifying namespaces to import
- dotnet/msbuild#6745
- Ensure the generated file is still emitted if any
<Using>
items are declared (even if<ImplicitUsings>
is false) - Ensure that implicit
<Import>
items for Visual Basic are not changed from what they were in .NET 5 - Update processing of
<Using>
items to support definingusing [alias]
andusing static [type]
as well asusing [namespace]
, e.g.:<Using Include="Microsoft.AspNetCore.Http.Results" Alias="Results" />
emitsglobal using Results = global::Microsoft.AspNetCore.Http.Results;
<Using Include="Microsoft.AspNetCore.Http.Results" Static="True" />
emitsglobal using static global::Microsoft.AspNetCore.Http.Results;
<Using Include="Microsoft.AspNetCore.Http" />
emitsglobal using global::Microsoft.AspNetCore.Http;
- Update the C# project templates to enable implicit usings for new .NET 6 projects, i.e. include
<ImplicitUsings>enable</ImplicitUsings>
in the.csproj
file
Example project file content after these changes:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<Using Include="My.Awesome.Namespace" />
</ItemGroup>
</Project>
Issue Analytics
- State:
- Created 2 years ago
- Reactions:21
- Comments:45 (34 by maintainers)
Top Results From Across the Web
C# 10.0 implicit global using directives
In this blog I'll show one of the new features supporting this: implicit global using directives (aka 'global imports'). The one problem ......
Read more >The Problem with C# 10 Implicit Usings
Yesterday I livestreamed myself upgrading a project to .NET 6 and C# 10. Along the way I tried using a new C# 10...
Read more >Is there a way to enable "Implicit Usings" feature when ...
The "ImplicitUsings" feature allows code to omit usings statements for standard namespaces. Is there a way to tell a C# compilation to ...
Read more >Implicit Using Statements In .NET 6 - NET Core Tutorials
With Implicit Using statements, your code will have almost invisible using statements declared globally! Let's take a look at this new feature, and...
Read more >C# 10: Disable Global Using
The presently accepted answer of disabling the ImplicitUsings MSBuild property does not affect the global using feature.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@DamianEdwards I do not think we should support
enable
as a value for theImplicitUsings
property. Other Boolean MSBuild properties just supporttrue
. TheNullable
property usesenable
instead oftrue
because it has 4 different supported values instead of just true and false: https://docs.microsoft.com/en-us/dotnet/csharp/nullable-references#nullable-contextsWhy is this not just driven by the template? Have an
_Usings.cs
in the project by default, like Razor does. Much easier to see what namespaces included by default. No need to google the documentation. Personally, I’d much rather just stay in my c# headspace and edit that file to add or remove namespaces, as opposed to mucking with the csproj file with a completely different syntax.