CS Project File Directives
Common .csproj file directives.
Order doesn’t matter within each Group.
Open each to learn more and for other options.
<Project Sdk=”Microsoft.NET.Sdk”>
Top-level, encloses all else. Sdk is what type of sdk this project uses. Other valid values:
- Microsoft.NET.Sdk.Web
- Microsoft.Net.Sdk.Worker
<PropertyGroup Label=”ex: Production or Dev”>
PropertyGroup controls the build process
Can have Label and Condition attributes
Example:
<PropertyGroup Condition="'$(CompileConfig)' == 'DEBUG'">
<Optimization> false </Optimization>
<Obfuscate>false</Obfuscate>
<OutputPath>$(OutputPath)\debug</OutputPath>
</PropertyGroup>
<OutputType>Exe</OutputType>
Library
<TargetFramework>net9.0</TargetFramework>
.Net 9.0 uses C# 13 by default
netstandard2.0 — used by .Net Framework, Xamarin, modern .Net
.Net Standard 2.0 is the x-platform standard
note it uses C# 7.3 by default but your can change it in LangVersion tag
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
Enables the compiler nullability warnings at the project level.
If omitted, it is set to disable by default
To enable/disable at file level write at top: #nullable enable or #nullable disable
<PublishAot>true</PublishAot>
Enables native AOT (Ahead-of-Time) OS-specific publishing
Must have Desktop development with C++ on VS installed
For Linux you must run sudo apt-get install clang zlib-dev
<InvariantGlobalization>true</InvariantGlobalization>
Makes the app non-culture-specific
To make the app act the same anywhere in the world
If false or missing, console app will take local culture of computer
<RootNamespace>Packt.Shared</RootNamespace>
substitute your default namespace for the project
<LangVersion>13</LangVersion>
C# version. If missing 13 will be used.
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
Allows unsafe code blocks (sizeof) for new types of numbers like so:
unsafe
{
Console.WriteLine($"{sizeof(Half)} bytes"});
}
<RuntimeIdentifiers>win-x64;osc-arm64;linux-x64</RuntimeIdentiers>
- win-x64 — Windows on an x64-compatible CPU
- osx-arm64 — macOS on Apple Silicon
- linux-x64 — most desktop distributions of Linux on x64-compatible CPU
- linux-arm — Raspbian or Raspberry Pi OS 32-bit
- linux-arm64 — Raspberry Pi Ubuntu 64-bit
</PropertyGroup>
<ItemGroup label=”whatever you want ex: Usings”>
<Using Include”System.Console” Static=”true” />
Now don’t need to add “using” declaration at top of each file
</ItemGroup>
<ItemGroup Label=”NuGet Packages”>
<ProjectReference Include=”[pathtoaprojectfile]” />
<ProjectReference Include=”Newstonsoft.Json” Version=”13.0.3″ />
To “fix” dependencies, every app should specify version without additional qualifiers
Qualifiers include: betas (beta1), release candidates (rc4), and wildcards.
Always fix dependencies to work with whatever version of .NET you are using
</ItemGroup>
</Project>