Alex Rivera | Logout

What is the C# equivalent to Java's isInstance()?

Asked 2008-11-11T23:09:50.523
95

I know of is and as for instanceof, but what about the reflective isInstance() method?

Edit
Report

1 Answer

39

Depends, use is if you don't want to use the result of the cast and use as if you do. You hardly ever want to write:

if(foo is Bar) {
    return (Bar)foo;
}

Instead of:

var bar = foo as Bar;
if(bar != null) {
    return bar;
}
answered 2008-11-12T05:30:53.157

Your Answer