I can't seem to wrap my head around the compiler's warning in this case:
using System;
using System.Collections.Generic;
#nullable enable
public class Program
{
public static void Main()
{
Guid guid = Guid.NewGuid();
Dictionary<Guid, string> d = new();
bool found = d.TryGetValue(guid, out string? str);
if (found is false)
{
return;
}
string s = str; // WARNING: possible null value
}
}
After all, I'm doing the found
check and return if there is no value (e.g. when the out str
value is null). Plus, the out
parameter of the .TryGetValue
method is annotated with [MaybeNullWhen(false)]
.
Would appreciate your help figuring our where I'm wrong in my expectations and fixing the code, thanks. Code is here.