views:

109

answers:

3

Are the following code snippets equivalent?

class a
{}

class b:a
{}

b foo=new b();

//here it comes

foo is a

//...is the same as...

typeof(a).isinstanceoftype(foo)

Or maybe one of the other Type Methods map closer to the is operator. e.g. "IsAssignableFrom" or "IsSubclassOf"

+3  A: 

It isn't, because is is tolerant to null reference at the left-hand side.

Ondrej Tucny
+3  A: 

It's not the same as is is translated into the isinst opcode whereas IsInstanceOf is a normal virtual call on Type.

FuleSnabel
+2  A: 

No it's not. In fact, if you peek into IsInstanceOfType you will see that the very first code line actually performs a comparison using is, which would effectively lead to a StackOverflowException if that was the case.

The is operator leads to an isinst operation in the IL code.

Fredrik Mörk
it seems so that the implementation of IsInstanceOfType got a lot simpler in .Net 4.0. It basically just delegates to IsAssignableFrom.
rudimenter