.NET project file analyzers community

Proj0003: Define usings explicit

Once enabled, depending on the type of project you have created you’ll have the following global using statements added to your project implicitly.

SDK Default namespaces
Microsoft.NET.Sdk  
  System
  System.Collections.Generic
  System.IO
  System.Linq
  System.Net.Http
  System.Threading
  System.Threading.Tasks
Microsoft.NET.Sdk.Web  
  System.Net.Http.Json
  Microsoft.AspNetCore.Builder
  Microsoft.AspNetCore.Hosting
  Microsoft.AspNetCore.Http
  Microsoft.AspNetCore.Routing
  Microsoft.Extensions.Configuration
  Microsoft.Extensions.DependencyInjection
  Microsoft.Extensions.Hosting
  Microsoft.Extensions.Logging
Microsoft.NET.Sdk.Worker  
  Microsoft.Extensions.Configuration
  Microsoft.Extensions.DependencyInjection
  Microsoft.Extensions.Hosting
  Microsoft.Extensions.Logging

Although this might sound like a good idea, there are downsides. It turns out that with multiple targets enabled (frameworks versions mainly) some of the included namespaces (like System.Net.Http) do not exist in your project unless a NuGet package is added.

But more importantly, it obscures the dependencies of your project. In addition, reducing using statements in your project can be easily achieved by defining namespaces globally in a file.

Non-compliant

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

</Project>

Compliant

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
  </PropertyGroup>

</Project>
global using System;
global using System.Collections.Generic;
global using System.IO;
global using System.Linq;
global using System.Net.Http;
global using System.Threading;
global using System.Threading.Tasks;