Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nullable in csproj - what's the default?

Tags:

c#

.net

I cannot find any documentation on the specs of csproj files.
I am especially interested in the <Nullable> property.
I think it used to be disabled by default, but starting with around C# 11 it is enabled by default -
How can I confirm this?

like image 300
mnj Avatar asked Oct 30 '25 06:10

mnj


1 Answers

From Nullable reference types documentation:

By default, nullable annotation and warning contexts are disabled. That means that your existing code compiles without changes and without generating any new warnings. Beginning with .NET 6, new projects include the enable element in all project templates.

So - for older projects, default is disabled, but since .Net 6 (C# 10) all project templates have nullable contexts enabled.

Update following Jon Skeet's comment -
The fact that the project templates since .Net 6 have nullable context enabled by default doesn't actually make the default behavior or nullable context to be enabled, and actually with a good reason - since older projects didn't even have that defined, the only way to keep backwards compatibility would be to treat non-existing specifications as disabled.

For instance, if you're creating a new Console app in .Net 6 this is the project template you'll get out of the box (at least on my VS 2022):

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

</Project>

If you remove the Nullable XML tag, you'll get the same effect as <Nullable>disable</Nullable>.

like image 76
Zohar Peled Avatar answered Nov 01 '25 20:11

Zohar Peled



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!