C# 'is' operator Clarification

前端 未结 6 1934
半阙折子戏
半阙折子戏 2021-01-17 21:11

Does the is operator indicate whether or not an object is an instance of a certain class, or only if it can be casted to that

6条回答
  •  孤独总比滥情好
    2021-01-17 21:20

    if(something is X) checks if the underlying type of something is X. This is significantly different from checking if a type supports casting to X since many types can support casts to X without being of type X.

    Conversely the as operator attempts a conversion to a particular type and assigns null if source type is not within the inheritance chain of the target type.

提交回复
热议问题