Sunday, June 02, 2013

What the meaning of is is

Today a follow-up to my 2010 article about the meaning of the is operator. Presented as a dialog, as is my wont!

I've noticed that the is operator is inconsistent in C#. Check this out:

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?

Let's suppose you and I are neighbours.

Um... ok, I'm not sure where this is going, but sure.


I'm looking into your driveway, which is empty. Can that driveway hold a Honda Civic?

Sure, no problem. In fact, it frequently does.

Now we cross the street and we look at my driveway, which is also empty. Can it hold a Honda Civic?

Yes, I suppose so.

In fact I have a rule that only Honda Civics may be parked in my driveway.

That's a bit weird, but that's your business.

Can the present contents of my driveway be placed in your driveway?

Um... I don't know. That's a strange question. On the one hand I want to say that the question presupposes a false premise, namely that "the present contents of my empty driveway" refers to an object that can be moved from one place to another. On the other hand, the contents of our two empty driveways seem to be by definition "the same", namely, "zero Honda Civics". If you insist on an answer then I suppose I will have to say yes, the present contents of your driveway fit into my driveway.

OK, so, does your driveway contain a Honda Civic?

Obviously not. It's empty.

Well, aren't you being inconsistent then? Let's review the facts.

You agree that my driveway only contains Honda Civics.
You agree that the present contents of my driveway are identical to the contents of your driveway.
And yet you conclude that your driveway does not contain a Honda Civic!
Either you are being inconsistent or there is something wrong with this logic.

I'd agree with that! There's something wrong with your logic.

And now we see why the is operator is actually consistent. The fact that a null reference may be assigned to a string variable does not make the null reference a string, any more than the fact that your driveway can be empty means that an empty driveway contains a Honda Civic. The is operator does not answer the question "can I assign this reference to a variable of the given type?" It answers the question "is this reference a legitimate reference to an object of the given type?", and null is not a legitimate reference.


QR: Inline image 1