Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Possible null reference after null check

I have a problem resolving this CS8603 warning. Even though there is a null-check for resource variable, null reference is still possible. How come?

enter image description here

Code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public string GetResource(string resourcePath)
{
    var resource = Application.Current.Resources[resourcePath];
    if (resource == null)
    {
        return $"{ResourceError} [{resourcePath}]";
    }

    // ToDo: CS8603
    return resource.ToString();
}

>Solution :

You did correctly check wether resource is null. But even if it is not, ToString() might return null. You can use something like

return resource.ToString() ?? "";
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading