1

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:

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

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

1 Answer 1

4

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() ?? "";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.