Skip to content

3.7 — SDK-Style .csproj, Target Frameworks, Multi-Targeting

← Roadmap · Section: 3. .NET Runtime and Platform Prev: 3.6 Assemblies and NuGet · Next: 3.8 The .NET CLI

Markers: ⭐ · Confidence: 🔴 · Last reviewed: —


TL;DR

The .csproj file is your project's build instructions. Modern .NET uses the SDK-style format, which is short because it assumes sensible defaults instead of listing every file.

  • <TargetFramework> says which .NET you build against. net10.0 is a TFM — Target Framework Moniker.
  • Files are included automatically by pattern. You do not list them.
  • Multi-targeting builds one project for several frameworks at once, which is how libraries support both old and new .NET.

Definitions

Term What it is What it means
.csproj an XML file The build definition for one project: what to target, what to reference, how to package.
SDK-style project the modern .csproj format Starts with <Project Sdk="...">. Short, because everything sensible is a default.
MSBuild the build engine Reads the .csproj and runs the build. The dotnet build command drives it.
TFM a short code string Target Framework Moniker. net10.0, net48, netstandard2.0. Says what you build against.
<TargetFramework> a property, singular One TFM. The normal case.
<TargetFrameworks> a property, plural Several TFMs, semicolon-separated. Triggers multi-targeting.
Multi-targeting a build mode Building the same source once per TFM, producing several assemblies.
RID a short code string Runtime Identifier. Names an OS and CPU: win-x64, linux-musl-arm64.
<PropertyGroup> an XML element Holds settings — single values like the target framework or version.
<ItemGroup> an XML element Holds lists — references, files, package dependencies.
Implicit usings a build feature The SDK adds common using lines automatically based on project type.
Directory.Build.props a shared file Settings applied to every project in a folder tree. Avoids repetition.
Conditional compilation a compiler feature #if NET10_0_OR_GREATER — include code only for certain targets.

The concept

Why the file got so much shorter

An old .NET Framework .csproj was hundreds of lines. It listed every single source file by name:

<!-- The OLD format — every file listed explicitly -->
<ItemGroup>
  <Compile Include="Program.cs" />
  <Compile Include="Models\Order.cs" />
  <Compile Include="Models\Customer.cs" />
  <Compile Include="Services\OrderService.cs" />
  <!-- ...and on, and on -->
</ItemGroup>

This caused constant pain. Adding a file meant editing the project. Two developers adding files at once caused a merge conflict in XML. Renaming a folder broke everything.

The SDK-style format replaced it. Here is a complete, working modern project file:

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

  <PropertyGroup>
    <TargetFramework>net10.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

</Project>

That is the whole thing. Every .cs file in the folder and below is included automatically by a wildcard pattern the SDK supplies. You never list files again.

The Sdk attribute at the top says which set of defaults to use:

SDK For
Microsoft.NET.Sdk Class libraries, console apps
Microsoft.NET.Sdk.Web ASP.NET Core apps
Microsoft.NET.Sdk.Razor Razor class libraries
Microsoft.NET.Sdk.Worker Background service apps

What a TFM actually is

A Target Framework Moniker is just a short code saying which .NET you are building against. It determines which APIs the compiler will let you use.

TFM Means
net10.0 .NET 10, cross-platform
net8.0 .NET 8, cross-platform
net48 .NET Framework 4.8, Windows only
netstandard2.0 The old shared-API bridge (3.9)
net10.0-windows .NET 10 plus Windows-only APIs — needed for WPF or WinForms

That last form is worth knowing. The suffix unlocks platform-specific APIs, and it means the resulting assembly only runs on that platform.


How it works

The simple version — the properties you will actually set

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

  <PropertyGroup>
    <TargetFramework>net10.0</TargetFramework>

    <!-- Turn on nullable reference type warnings — see 1.15 -->
    <Nullable>enable</Nullable>

    <!-- Add common usings automatically, so files start at the namespace -->
    <ImplicitUsings>enable</ImplicitUsings>

    <!-- Treat warnings as errors. Worth doing in CI. -->
    <TreatWarningsAsErrors>true</TreatWarningsAsErrors>

    <!-- Produce the XML docs file so consumers get IntelliSense -->
    <GenerateDocumentationFile>true</GenerateDocumentationFile>

    <!-- Required for pointers — see 3.3 -->
    <AllowUnsafeBlocks>false</AllowUnsafeBlocks>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Serilog.AspNetCore" Version="8.0.0" />
    <ProjectReference Include="../MyApp.Core/MyApp.Core.csproj" />
  </ItemGroup>

</Project>

<PropertyGroup> holds single settings. <ItemGroup> holds lists. That is the only structural rule you need.

Implicit usings — where did the using lines go?

With <ImplicitUsings>enable</ImplicitUsings>, the SDK adds a standard set of using directives to every file, chosen by project type. A web project gets things like:

global using System;
global using System.Collections.Generic;
global using System.Linq;
global using System.Threading.Tasks;
global using Microsoft.AspNetCore.Builder;
// ...and more

This is why a modern file often starts straight at the namespace declaration with no using block. You can add your own project-wide ones:

<ItemGroup>
  <Using Include="MyApp.Common.Extensions" />
  <Using Include="System.Text.Json" Alias="Json" />
</ItemGroup>

⚠️ The downside is discoverability. When a type resolves and you cannot see why, the using is not in the file you are reading. That is worth knowing when someone new joins the project and cannot work out where something came from.

How it actually works — multi-targeting

A library often needs to support several .NET versions. Multi-targeting builds it once per target:

<PropertyGroup>
  <TargetFrameworks>net10.0;net8.0;netstandard2.0</TargetFrameworks>
</PropertyGroup>

Note the plural element name. That single change produces three separate assemblies, and dotnet pack puts all three in one NuGet package. Consumers automatically get the one matching their project.

Since each build sees a different set of APIs, you need conditional compilation where they differ:

public static string Describe(ReadOnlySpan<char> input)
{
#if NET8_0_OR_GREATER
    // Available on modern .NET only
    return string.Concat("Value: ", input);
#else
    // netstandard2.0 fallback
    return "Value: " + input.ToString();
#endif
}

The SDK defines these symbols for you: NET10_0, NET8_0_OR_GREATER, NETSTANDARD2_0, NETFRAMEWORK and so on. Prefer the _OR_GREATER forms — #if NET8_0 is false when you later add net10.0, which produces a very confusing regression.

You can also vary references per target:

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
  <!-- Only the old target needs this polyfill package -->
  <PackageReference Include="System.Text.Json" Version="8.0.5" />
</ItemGroup>

Only multi-target when you actually need to. It multiplies build time and testing effort. An application should target exactly one framework — multi-targeting is for libraries with consumers you do not control.

The deeper detail — sharing settings across a solution

Repeating the same properties in fifteen project files is a maintenance problem. Two files solve it.

Directory.Build.props at the solution root applies to every project beneath it:

<Project>
  <PropertyGroup>
    <TargetFramework>net10.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
    <LangVersion>latest</LangVersion>
  </PropertyGroup>
</Project>

Individual projects then only carry what makes them different.

Directory.Packages.props centralises package versions so they cannot drift apart:

<Project>
  <PropertyGroup>
    <ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
  </PropertyGroup>
  <ItemGroup>
    <PackageVersion Include="Serilog.AspNetCore" Version="8.0.0" />
    <PackageVersion Include="FluentValidation" Version="11.9.0" />
  </ItemGroup>
</Project>

Projects then reference without a version:

<PackageReference Include="Serilog.AspNetCore" />

This directly prevents the diamond dependency problem from 3.6 within your own solution, and it is worth setting up on anything with more than about three projects.

RIDs and publishing

A Runtime Identifier names a specific OS and CPU. You need one when publishing something self-contained:

dotnet publish -c Release -r linux-x64 --self-contained

Common values: win-x64, linux-x64, linux-musl-x64 (Alpine), osx-arm64.

⚠️ linux-musl-x64 is the Alpine one. Building linux-x64 and running it in an Alpine container fails at startup with a confusing missing-library error, because Alpine uses musl rather than glibc. This catches people out regularly — see 24.3.


Code

A realistic library that supports both modern .NET and .NET Framework consumers:

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

  <PropertyGroup>
    <TargetFrameworks>net10.0;netstandard2.0</TargetFrameworks>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>

    <PackageId>MyCompany.Orders</PackageId>
    <Version>2.1.0</Version>
    <GenerateDocumentationFile>true</GenerateDocumentationFile>
    <PackageLicenseExpression>MIT</PackageLicenseExpression>
  </PropertyGroup>

  <!-- netstandard2.0 predates nullable annotations, so it needs a shim -->
  <ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
    <PackageReference Include="Nullable" Version="1.3.1" PrivateAssets="all" />
    <PackageReference Include="System.Text.Json" Version="8.0.5" />
  </ItemGroup>

</Project>

PrivateAssets="all" means that package is used to build this library but not passed on to whoever consumes it — right for build-time helpers and analysers.


Interview questions

Q: What is the difference between the old and SDK-style project formats?

The old format listed every source file explicitly, which made project files hundreds of lines long and caused merge conflicts whenever two people added files. SDK-style projects include files automatically by wildcard, so a complete modern project file can be six lines. The Sdk attribute at the top selects a set of defaults appropriate to the project type — web, library, worker. The practical difference day to day is that you almost never edit the project file except to add a package or change a property.

Q: What is a TFM?

A Target Framework Moniker — a short code in the project file saying which .NET version you build against, like net10.0 or net48. It controls which APIs the compiler allows. There is also a platform-suffixed form such as net10.0-windows, which unlocks Windows-only APIs for things like WinForms and means the output only runs on Windows.

Q: What is multi-targeting and when would you use it?

Building one project against several target frameworks at once, using the plural <TargetFrameworks> property. The compiler runs once per target, producing several assemblies that dotnet pack bundles into a single NuGet package, and consumers automatically get the one matching their project. You need it for libraries that must support both modern .NET and older consumers. Where the APIs differ you use conditional compilation with symbols like NET8_0_OR_GREATER. Applications should target exactly one framework — multi-targeting costs build time and testing effort, so it is only worth it when you do not control your consumers.

Q: How do you avoid repeating settings across many projects?

Directory.Build.props at the solution root, which MSBuild applies to every project beneath it — so target framework, nullable settings and language version live in one place. For package versions there is Directory.Packages.props with central package management, where projects reference a package without a version and the version is declared once. That also prevents the situation where two projects in the same solution drift onto different versions of the same dependency.


Traps ⚠️

  • <TargetFramework> versus <TargetFrameworks>. One letter changes single-target into multi-target, and the error if you get it wrong is unhelpful.
  • #if NET8_0 breaks when you add a newer target. Use NET8_0_OR_GREATER.
  • Implicit usings hide where types come from. The using is not in the file you are reading.
  • Multi-targeting multiplies your test matrix. Only do it for libraries that need it.
  • linux-x64 does not run on Alpine. Alpine needs linux-musl-x64.
  • Files are included by wildcard, so a stray .cs file anywhere under the project folder gets compiled — including in a backup folder someone left behind.
  • Forgetting PrivateAssets="all" on build-time packages leaks them to your consumers.
  • Changing the TFM can silently change behaviour, because the SDK applies different defaults per version.


Sources