c# – How do I modify the template for the auto-generated global using file?


Per Selvin’s comment what you probably DON’T want to modify is:

dotnet\sdk\VERSION\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.CSharp.props

which serves as the base source for the auto-generated usings file:

<!-- Implicit imports -->
<ItemGroup Condition="'$(ImplicitUsings)' == 'true' Or '$(ImplicitUsings)' == 'enable'">
  <Using Include="System" />
  <Using Include="System.Collections.Generic" />
  <Using Include="System.IO" />
  <Using Include="System.Linq" />
  <Using Include="System.Net.Http" Condition="'$(TargetFrameworkIdentifier)' != '.NETFramework'"/>
  <Using Include="System.Threading" />
  <Using Include="System.Threading.Tasks" />
</ItemGroup>

However, your code will have issues when you

  1. Upgrade the SDK on your machine
  2. Try to build it on another machine where you haven’t manually modified the SDK files

The the recommended alternative is a “specifically named” GlobalUsings.cs file that you just copy-paste to new projects where you put those extra usings:

global using System.Diagnostics;

Leave a Reply

Your email address will not be published. Required fields are marked *