When evaluating your statement, the runtime must first follow the link to which the variable belongs. Only then can he evaluate the object referenced to determine if this is a string.
Since the null reference refers to the absence of an object, this is not a string. In fact, this does not mean anything.
You can use the typeof operator to get a Type object that matches the string, instead of comparing the reference object if that is your ultimate goal.
This is actually a concrete example given by Eric Lippert on a blog post on this subject:
I noticed that the C operator is incompatible with C #. Check this:
string s = null; // Clearly null is a legal value of type string bool b = s is string; // But b is false!
What's up with that?
- http://ericlippert.com/2013/05/30/what-the-meaning-of-is-is/
David pfeffer
source share