Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

894
Visualizações
Resolve compilation error “CS8601 Possible null reference assignment”

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

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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).

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda