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

前端 未结 5 911
清歌不尽
清歌不尽 2020-12-13 16:54

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

5条回答
  •  一整个雨季
    2020-12-13 17:18

    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;
    }
    

提交回复
热议问题