Resolve compilation error “CS8601 Possible null reference assignment”
Asked Answered
C

2

13

Since C# 8.0, the standard is non nullable variable by default. Ref1

But in case of generics like this one:

    public static T PerfomIO<T>(Func<T> function, T defaultValue = default)
    {
        try
        {
            return function();
        }
        catch (IOException)
        {
        }

        return defaultValue;
    }

How to get rid of the compilation error "CS8601 Possible null reference assignement" that occur when I try to pass "default" ?

I want to support null value here. I do not want to disable the error message. I want to program it the way it should be.

I try to add Nullable in many ways without success. I try [AllowNull] without success

enter image description here

Corbitt answered 6/4, 2021 at 17:56 Comment(4)
You may look at nullable analysis attributesPergolesi
That's not an error, is it (definietly not an exception)? Should only be a warning.Ride
Have you tried removing the assignment and creating an overload with a single argument that calls this method with the default for the type? Not sure if that would help or not (but it should since the assignment is removed), and the functionality would be the same. i.e. public static T MyFunc<T>(Func<T> function) { return MyFunc(function, default); }Ride
This question is being discussed on Meta: meta.https://mcmap.net/q/905848/-wpf-design-rendering-is-dramaticaly-slow-visual-studio-2008-are-there-any-tweaks-that-could-speed-it-upChartography
C
18

I found a few tricks although I had no idea why the project was forced to "nullable check" (found and details below). I look in the .csproj, .sln and elsewhere and didn't find any parameter that could force the null check.

I found 2 ways to workaround the problem I had:

  • Solution 1: You can add the next line at the start of your .cs file:

    #nullable disable

  • Solution 2: Change the parameter from default to default! where the ! tells the compiler not to consider the nullable check.

I currently use default!.

Also just as reference, if your project forces null check but you can't find the option in your project or in your solution, then look for this article: Customize your build, perhaps you will find why the null check is forced. (All new since 2019).

Corbitt answered 11/4, 2021 at 18:19 Comment(0)
T
0

In Visual Studio 2022 you can add .editorconfig file to suppress those annoying messages (I sure hope you know what you are doing)

In the .sln file:

Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{32FE27E8-3094-4A72-8200-3A2639355EF0}"
        ProjectSection(SolutionItems) = preProject
            .editorconfig = .editorconfig
        EndProjectSection
    EndProject

and .editorconfig file content is:

[*.cs]

# CS8600: Converting null literal or possible null value to non-nullable type.
dotnet_diagnostic.CS8600.severity = none

There is a way to create this file and hook it up suing VS2022 by hovering over the issue:

suppress CS8600

I hope it will help someone 🙂

Tilda answered 5/7, 2022 at 0:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.