Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you turn off the nullable reference type feature(s)?

Tags:

c#-8.0

I just started my first project with c#8 and immediately I'm hit with warnings everywhere on my properties. After some research, it looks like I'm now supposed to explicitly add a ? to any properties that can be null.

Two questions:

  1. If I port code from previous versions to this version, does this actually affect runtime in anyway whatsoever, or do I just have to ignore a bunch of warnings?

  2. Is there a simple switch to revert to the old behavior so it won't show these warnings? I understand that there are ways I could update my code in a million places to ignore the warnings... I don't want to do that. It's a waste of time and I'd rather live with them if they don't actually affect anything at runtime. But if there is something I can do in a single place to get rid of them that would be great.

like image 365
BVernon Avatar asked Aug 30 '25 14:08

BVernon


1 Answers

  1. The nullability analysis feature only reports warnings, it doesn't affect runtime in any way.
  2. Yes you can suppress those diagnostics, <Nullable>disable</Nullable> in the project file or #nullable disable at the top of your source file will do it.

example .csproj file:

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

  <PropertyGroup>
    <TargetFramework>net9.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>disable</Nullable>  <!-- this -->
    <AssemblyTitle>whatever....</AssemblyTitle>
  </PropertyGroup>

like image 103
Julien Couvreur Avatar answered Sep 10 '25 11:09

Julien Couvreur