Using C#8, Visual Studio 2019 16.7.2, given the following C# code:
#nullable enable
public async Task<string> GetStringAsync(); ...
public async void Main()
{
var theString = await GetStringAsync();
...
}
Intellisense hovering over theString
shows a tooltip of local variable (string?) theString
My GetStringAsync
method never returns a nullable string, yet the variable is inferred as being nullable.
Is this an intellisense bug? Or is there something deeper going on where theString
can actually be null due to some way that await
works?
This is by-design and does not have to do with the await
.
var
is always nullable, from the spec:
var
infers an annotated type for reference types. For instance, invar s = "";
thevar
is inferred asstring?
.
As such, you can't explicitly specify theString
as non-nullable when you use var
. If you want it to be non-nullable, use string
explicitly.
As to why: In short, it's to allow scenarios like this:
#nullable enable
public async Task<string> GetStringAsync(); ...
public async void Main()
{
var theString = await GetStringAsync();
if (someCondition)
{
// If var inferred "string" instead of "string?" the following line would cause
// warning CS8600: Converting null literal or possible null value to non-nullable type.
theString = null;
}
}
The compiler will use flow analysis to determine whether or not the variable is null at any given point. You can read more about the decision here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With