question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Update implicit global usings feature to address issues

See original GitHub issue

The 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 defining using [alias] and using static [type] as well as using [namespace], e.g.:
    • <Using Include="Microsoft.AspNetCore.Http.Results" Alias="Results" /> emits global using Results = global::Microsoft.AspNetCore.Http.Results;
    • <Using Include="Microsoft.AspNetCore.Http.Results" Static="True" /> emits global using static global::Microsoft.AspNetCore.Http.Results;
    • <Using Include="Microsoft.AspNetCore.Http" /> emits global 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
    • This will be tracked by issues in the relevant repos for each template
    • dotnet/aspnetcore#35131
    • dotnet/templating#3619
    • dotnet/winforms#5074

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:closed
  • Created 2 years ago
  • Reactions:21
  • Comments:45 (34 by maintainers)

github_iconTop GitHub Comments

9reactions
dsplaistedcommented, Aug 10, 2021

@DamianEdwards I do not think we should support enable as a value for the ImplicitUsings property. Other Boolean MSBuild properties just support true. The Nullable property uses enable instead of true because it has 4 different supported values instead of just true and false: https://docs.microsoft.com/en-us/dotnet/csharp/nullable-references#nullable-contexts

7reactions
jmailletcommented, Aug 24, 2021

Why 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found