As part of moving towards C# 8 nullable reference types, I encountered the following code (simplified):
public string GetIfExists(string key)
{
dict.TryGetValue(key, out var value);
return value;
}
The return line warns on a possible null reference return, and it makes sense. So I tried to annotate the method with a [return: MaybeNull] attribute, but the warning remained to my surprise. From the documentation, I understand that this attribute marks the return type as optional null, even when the actual type doesn't allow it.
It seems that my only option to avoid warnings is to mark the return type string?. So what is the use of [return: MaybeNull]?
[return: MaybeNull]on your method, and the warning went away. Can you show the exact code you tried, and then also show a screenshot of the warning still being there after a rebuild? - Lasse V. Karlsen