Home > Software design >  Non-Nullable becomes Nullable on return
Non-Nullable becomes Nullable on return

Time:02-07

I think I've seen this question answered somewhere, but I can't find it and don't remember the answer.

Assuming Nullable-Reference-Types is enabled.

There is a method that returns a string. The caller of the method stores the resulting string into a variable. The variable will be Nullable, even if the method's return value is non-nullable.

static string MyMethod() 
{
   return "Hello World!";
}

public static void Main() 
{
   // result is of type string? here. Hence nullable.
   var result = MyMethod();

   // result2 is also nullable here.
   string result2 = MyMethod();
}

The same does not seem to happen with primitive types, but it does happen reference types, like object. Kind of defeats the purpose of NRT for me. I'm sure there's a reason for this and it's "by design".

CodePudding user response:

This is indeed by design.

The meeting notes where this design was chosen are here: https://github.com/dotnet/csharplang/blob/main/meetings/2019/LDM-2019-12-18.md#var

To summarise: It was found that people were frequently having to explicitly type the variable rather than using var because after assignment from a non-null-returning method, the code went on to assign null to the variable.

To quote from the meeting notes:

At this point we've seen a large amount of code that requires people spell out the type instead of using var, because code may assign null later.

That is, code like the following was found to be quite frequent:

var someVariable = SomeMethodThatDoesntReturnNull();
...
someVariable = null; // or someVariable = someMethodThatCanReturnNull();

Note that (as pointed out by Jeroen Mostert) result2 in your question is NOT nullable because it's explicitly typed as non-nullable. I'm not sure why you think it is nullable, but it definitely isn't.

For example, look at this repro on DotNetFiddle and note the warning on line 11.

CodePudding user response:

In C# 8.0, strings are known as a nullable “string!”, and so the Allow Null annotation allows setting it to null, even though the string that we return isn't null (for example, we do a comparison check and set it to a default value if null.)

  •  Tags:  
  • Related