With ImplicitUsings enabled in a .NET 10 Microsoft.NET.Sdk.Web project, which namespaces are imported automatically, and how do you change that list?
answer
- seven base plus nine web
- Using items in the SDK props
- obj/<Project>.GlobalUsings.g.cs
- <Using Include> and <Using Remove>
- Microsoft.AspNetCore.Mvc is not in it
basics
~10 sThe SDK generates global using directives into obj/<Project>.GlobalUsings.g.cs: seven System namespaces plus nine web ones such as Microsoft.AspNetCore.Builder and Microsoft.Extensions.DependencyInjection. Using items in the csproj add, alias or remove entries.
solid answer
~30 s`ImplicitUsings` set to `enable` (or `true`) turns on `Using` items the SDK declares. The base SDK adds `System`, `System.Collections.Generic`, `System.IO`, `System.Linq`, `System.Net.Http`, `System.Threading` and `System.Threading.Tasks`; the Web SDK adds `System.Net.Http.Json`, `Microsoft.AspNetCore.Builder`, `.Hosting`, `.Http`, `.Routing`, and `Microsoft.Extensions.Configuration`, `.DependencyInjection`, `.Hosting`, `.Logging`. Before compiling, the `GenerateGlobalUsings` target writes them as `global using global::...;` lines into `obj/.../<Project>.GlobalUsings.g.cs`. You add one with `<Using Include="..." />`, alias with `Alias=`, import a static type with `Static="True"`, and drop one with `<Using Remove="..." />`. Namespaces such as `Microsoft.AspNetCore.Mvc`, `Microsoft.Extensions.Options` and `Microsoft.EntityFrameworkCore` are not in the list.
code
xml · 6 lines<ItemGroup>
<Using Include="Microsoft.AspNetCore.Mvc" />
<Using Include="Microsoft.Extensions.Options" />
<Using Include="Microsoft.AspNetCore.Http.Results" Alias="Results" />
<Using Remove="System.Net.Http" />
</ItemGroup>go deeper
Recall that implicit usings are why Program.cs has no using lines, and that controller attributes still need using Microsoft.AspNetCore.Mvc.
Explain that the list is MSBuild Using items per SDK, generated into obj as GlobalUsings.g.cs, and how Include, Remove, Alias and Static change it.
Use the generated file to diagnose ambiguous references after a package upgrade, and keep shared usings in Directory.Build.props rather than per file.
Set a solution-wide convention for global usings so libraries and web projects resolve names the same way and moves between them stay cheap.
## What the feature is A **global using directive** (`global using X;`) applies to every file in a compilation. The language feature belongs to C#; what the .NET SDK adds is **implicit usings**: a default set of global usings per SDK, switched on by one MSBuild property. Every ASP.NET Core template writes `<ImplicitUsings>enable</ImplicitUsings>` into its `.csproj`, which is why a template's `Program.cs` can start with `var builder = WebApplication.CreateBuilder(args);` and no `using` lines at all. ## Exactly which namespaces arrive The list is not hard-coded in the compiler; it is a set of `<Using Include="..." />` items in the SDKs' `.props` files, guarded by `'$(ImplicitUsings)' == 'true' or 'enable'`. | Source | Namespaces | |---|---| | `Microsoft.NET.Sdk` | `System`, `System.Collections.Generic`, `System.IO`, `System.Linq`, `System.Net.Http`, `System.Threading`, `System.Threading.Tasks` | | `Microsoft.NET.Sdk.Web` adds | `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` | Those nine cover what `Program.cs` touches: `WebApplication` (Builder), `IWebHostEnvironment` (Hosting), `HttpContext`, `Results` and `TypedResults` (Http), route builders (Routing), `IConfiguration`, `AddSingleton` and friends, `IHostEnvironment` extensions such as `IsDevelopment()`, and `ILogger<T>`. What is **not** there surprises people: - `Microsoft.AspNetCore.Mvc` — so `ControllerBase`, `[ApiController]` and `[HttpGet]` need `using Microsoft.AspNetCore.Mvc;` (the `webapi --use-controllers` template writes it into its controller); - `Microsoft.AspNetCore.Authorization` — `[Authorize]` needs a `using`; - `Microsoft.Extensions.Options`, `System.Text.Json`, `System.Diagnostics`; - `Microsoft.EntityFrameworkCore` — EF Core is a package, not part of the framework. ## Where the directives end up Before `CoreCompile`, the SDK's `GenerateGlobalUsings` target turns the `Using` items into a C# file and adds it to the compilation: 1. it collects every `Using` item (the SDK's plus your own); 2. it writes `global using global::System;` style lines to `$(IntermediateOutputPath)$(MSBuildProjectName).GlobalUsings.g.cs` — for a Debug build of `ParcelLockers.Api` that is `obj/Debug/net10.0/ParcelLockers.Api.GlobalUsings.g.cs`; 3. it includes that file as a `Compile` item. Opening that file is the quickest way to answer "why does this name resolve?" — the file is generated, so edit the project file, never the `.g.cs`. ## Changing the list The same `Using` item does every job: - **Add** a namespace for every file: `<Using Include="Microsoft.AspNetCore.Mvc" />`. - **Alias** a type or namespace: `<Using Include="Microsoft.AspNetCore.Http.Results" Alias="Results" />` emits `global using Results = global::Microsoft.AspNetCore.Http.Results;`. - **Import statics**: `<Using Include="Microsoft.AspNetCore.Http.Results" Static="True" />` emits `global using static ...`. - **Remove** one the SDK added: `<Using Remove="System.Net.Http" />`, for example when one of your own types collides with a name in it. - **Turn the feature off** with `<ImplicitUsings>disable</ImplicitUsings>`; your own `Using` items still generate the file. A hand-written `GlobalUsings.cs` with `global using` lines works too; the MSBuild items simply keep the list in the project file, where reviewers and `Directory.Build.props` can see it. ## Why it matters in practice - An ambiguous-reference error after adding a package often comes from an implicit namespace colliding with the package's type names; `Using Remove` or an alias fixes it without touching every file. - A class library on `Microsoft.NET.Sdk` gets only the seven base namespaces even if it references `Microsoft.AspNetCore.App`, so code moved out of a web project suddenly needs `using Microsoft.AspNetCore.Http;`. - A solution-wide `Directory.Build.props` can hold shared `Using` items, so every web project imports `Microsoft.AspNetCore.Mvc` or `Microsoft.Extensions.Options` the same way without a per-file `using`. - Code review gets quieter: the namespaces every file needs disappear from the top of each file, and the ones that remain point at what is unusual about that file. ## Interview angle The question usually arrives as "Where are the `using` statements in `Program.cs`?" A strong answer names the property (`ImplicitUsings`), the mechanism (`Using` items generated into `GlobalUsings.g.cs` under `obj`), the per-SDK difference (seven base namespaces, nine more for the Web SDK), one namespace that is **not** included (`Microsoft.AspNetCore.Mvc`), and how to change the list from the project file. Mentioning that the C# feature underneath is `global using` — and that the SDK only generates such directives — shows the candidate knows which layer does what.
- Code moved from a .NET 10 web project into a Microsoft.NET.Sdk class library no longer compiles: HttpContext is unknown. Why?The library gets only the base SDK's seven implicit namespaces; `Microsoft.AspNetCore.Http` is one of the Web SDK's additions. Add `using Microsoft.AspNetCore.Http;` or a `<Using Include="Microsoft.AspNetCore.Http" />` item, plus a `FrameworkReference` to `Microsoft.AspNetCore.App` if the library lacks one.
- How do you see exactly which global usings a .NET 10 project compiles with?Build once and open `obj/<Configuration>/<TargetFramework>/<Project>.GlobalUsings.g.cs`. The SDK's `GenerateGlobalUsings` target writes every `Using` item there before compilation.
- Microsoft.AspNetCore.Mvc is implicit, so controllers need no using directive
- ImplicitUsings is a compiler switch with a fixed list you cannot change
- Edit GlobalUsings.g.cs in obj to add a namespace
- A Microsoft.NET.Sdk library referencing Microsoft.AspNetCore.App gets the web implicit usings
- Turning ImplicitUsings off deletes your own Using items too