9

I'm currently migrating a Blazor project to .NET6 which comes with C# 10. With the new language version I'm getting a warning with the following code:

if (tmp is null)
    oldValue = "";
else
    oldValue = tmp.ToString();

The warning is: CS8600 "Converting null literal or possible null value to non-nullable type". tmp is of type object?. That is the reason why I'm checking if it is null in the if clause. I have noticed that many warnings of this type occur.

So what is the reason behind that? The Help page does not contain any information yet. (see here)

UnusualWays
  • 177
  • 1
  • 2
  • 11
  • 11
    `object.ToString` have `string?` return type. Try `oldValue = tmp?.ToString() ?? ""` – JL0PD Dec 07 '21 at 10:16
  • 5
    And for an exciting discussion about why `object.ToString()` is annotated as returning `string?` rather than `string`, see here: https://github.com/dotnet/coreclr/pull/23466 – Matthew Watson Dec 07 '21 at 10:22
  • 2
    @Satpal `is` always checks against `null` while `==` *may* call overloaded operator – JL0PD Dec 07 '21 at 10:29
  • @UnusualWays Does your IDE suggest any automatic refactoring? In Visual Studio, you might be able to click on a light bulb and let the IDE fix the issue. – ˈvɔlə Dec 07 '21 at 10:31
  • @JL0PD Although `ToString()` has the return type `string?`, I think by convention it should never return null, therefore it's likely safe to just do `oldValue = tmp.ToString()!`. – PMF Dec 07 '21 at 10:35
  • @PMF, "In theory, theory and practice are the same. In practice, they are not.". Even though by convention there's can't be null, actually it can be and this will shoot your legs off sooner or later – JL0PD Dec 07 '21 at 10:40
  • 2
    @JL0PD - you can make that 1st comment an answer. – H H Dec 07 '21 at 11:14
  • I am not as interested in how to manage the warning as much as I am interested in why this is a warning. Why is is a bad thing to set objects' values to null? – Skip Saillors Feb 27 '22 at 18:04

4 Answers4

5

It appears that CS8600 warning is thrown when the compiler is not sure if the type is nullable or not. If you cast oldValue = (string?) tmp; the compiler is happy and the warning goes away.

Jim Broiles
  • 353
  • 2
  • 12
2

You can use the Coalesce operator (edited with @Alex Kalabukhov correction):

string oldValue = tmp?.ToString() ?? "Tmp to string is null";

This expression set the oldValue variable to tmp.ToString() if this is not NULL, or tmp = "Tmp to string is null if is null.

References:

  1. Microsoft documentation
  2. This page is in Spanish but it explains it better than the official documentation.
F A Fernández
  • 101
  • 1
  • 8
1

you have to cast to non nullable, like this

    int? tmp = 0;
    
    string oldValue = tmp == null ? "" : ((int)tmp).ToString();

or using your syntax

    string oldValue;

    if (tmp is null)
        oldValue = "";
    else
        oldValue = ((int) tmp).ToString();
Serge
  • 40,935
  • 4
  • 18
  • 45
1

In your file explorer you should have a file with .csproj extension. there you can find at row 7 the option nullable, write disable instead of enable. Correct File

giova
  • 11
  • 3