Question
How do I avoid Dereference of a possibly null reference?
(Using ASP.NET Core 7 and VS2022)
I have a utility function that I'm bringing over from an old .NET project:
The original looked like this:
public static string WriteString(object str)
{
if (str == null)
return "";
else
return str.ToString().Trim();
}
I converted it to this:
public static string WriteString(object str)
{
object s = str ?? "";
return s.ToString().Trim();
}
But I'm still getting the "Dereference of a possibly null reference" warning. Am I missing something? My variables can't possibly be null, because I'm using the null coalescing operator. So what's bothering VS?