C# : 'is' keyword and checking for Not

后端 未结 12 1997
没有蜡笔的小新
没有蜡笔的小新 2020-12-02 05:23

This is a silly question, but you can use this code to check if something is a particular type...

if (child is IContainer) { //....

Is ther

12条回答
  •  执笔经年
    2020-12-02 06:13

    While the IS operator is normally the best way, there is an alternative that you can use in some cirumstances. You can use the as operator and test for null.

    MyClass mc = foo as MyClass;
    if ( mc == null ) { }
    else {}
    

提交回复
热议问题