0

I try:

var err = (string)null;

(which I prefer to string? err=null;)


I get

warning CS8600: Converting null literal or possible null value to non-nullable type.

But the type of err is correctly set to a nullable string as is evident by hovering over it in visual studio.

Is this warning a dud ?

1 Answer 1

6

Assuming you have nullable reference types turned on, the warning is correctly pointing out that you are casting null to a non-nullable type - string. It is not about the type of err being non-nullable. err is nullable, as vars always are.

If you cast to string? instead, which is nullable, then the warning goes away:

var err = (string?)null;
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.