Home > Enterprise >  enabling nullable feature has no effect
enabling nullable feature has no effect

Time:12-13

While I was upgrading a project recently, I found this issue, and could'nt figure out what was up, so I reproduced it with a very simple console template starter app in visual studio 2022.

So, let's say I created the application from a console app template, and it is set to dotnet 3.1 core. It has this main method:

internal class Program
{
    static void Main(string[] args)
    {
        var person = new personModel();
        person.name = "Test";
        Console.WriteLine(person.name);
    }
}

and this model, in another file:

internal class personModel
{
    public string status { get; set; }
    public string name { get; set; }
}

So, I now update the project to .NET 6, this should enable Null-state analysis and variable annotations, and I now expect to receive warnings on the two strings that I have declared.

But, If I change the csproj file to the following:

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
  </PropertyGroup>

</Project>

The appplication will still build with 0 warnings. I'm not getting any sguiggly lines either. Ok, so I enable it manually:<nullable>enable</nullable> But I still build and run with absolutely no warnings anywhere. Why could this feature have no effect?

EDIT

It seems I was a bit too unclear with what I did. The last thing I tried was adding <nullable>enable</nullable> such that the csproj file looks like this:

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
      <nullable>disable</nullable>
  </PropertyGroup>

</Project>

CodePudding user response:

Add <Nullable>enable</Nullable> inside

<PropertyGroup><PropertyGroup>

in your .csproj file

Like :

<PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
</PropertyGroup>

CodePudding user response:

My suggestion is: the codebase should already have been using C# 8 or later version – because nullable reference types was not supported on previous versions of C#.

To check it, right-click in the project, go to properties and check the language version. I guess that your current language version starts as 7.3 or something else.

CodePudding user response:

You definitely need to add the <nullable>enable</nullable> property, as you did, after that you should get the warnings. These latest VS versions are somewhat buggy, it has nice new features, but it needs some fine tunning.

I find that adding or modifying properties tends to be more reliable on the configurations menus:

  • Right click on the project, select Properties -> Build -> General, you have a Nullable section with a drop down list, select Enable.

  • Right click on the project, select Properties -> Code Analysis and check Run on build and Run on live analisys on the All analysers section.

You should make sure all the settings are correct, meaning Target Framework 6.0, Language C# 10, etc.

If these are all ok, try disabling and enabling them again.

If it doesn't work try cleaning/rebuilding the solution after the changes.

CodePudding user response:

From the docs:

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 <Nullable>enable</Nullable> element in all project templates.

So the sole update to .NET 6 and corresponding language version will not help, you need opt-in for example by adding to the .csproj:

<PropertyGroup>
  <Nullable>enable</Nullable>
</PropertyGroup>
  • Related