Use of IsAssignableFrom and “is” keyword in C#

后端 未结 4 2065
谎友^
谎友^ 2020-12-03 02:41

While trying to learn Unity, I keep seeing the following code for overriding GetControllerInstance in MVC:

if(!typeof(IController).IsAssignableF         


        
4条回答
  •  南笙
    南笙 (楼主)
    2020-12-03 03:06

    It's not the same.

    if(controllerType is IController)
    

    would always evaluate to false since controllerType is always a Type, and a Type is never an IController.

    The is operator is used to check whether an instance is compatible to a given type.

    The IsAssignableFrom method is used to check whether a Type is compatible with a given type.

提交回复
热议问题