Google Search: c# instanceof

A couple of people have come to my site with the query “c# instanceof”. I know how I get found for this. I copied an “instanceof” javascript method and talked about it in one of my entries.

The only relation to c# that I can think of would be the “is” and “as” keywords.

“is” can be used to test what type of object an object reference is. For instance [no pun intend, or thought of I think :)] a developer could write:
if(m_car is MiniCooper)

they would be saying that if the m_car variable is of the type MiniCooper then do something.

The “as” keyword is used to “cast” a type to another type. So you could write

if(m_car is MiniCooper)
MiniCooper mc = m_car as MiniCooper;

The “as” keyword is slightly differnt to the Explicit cast (MiniCooper mc = (MiniCooper) m_car; because if the cast dosn’t work with an “as” then the mc local variable will be null, rather than throwing an “InvalidCastException”.

I hope this helps.

Paul Kinlan

Trying to make the web and developers better.

RSS Github Medium