What is the difference between typeof and the is keyword?

前端 未结 6 1041
太阳男子
太阳男子 2021-01-03 19:11

What\'s the exact difference between the two?

// When calling this method with GetByType()

public bool GetByType() {
    // this ret         


        
6条回答
  •  抹茶落季
    2021-01-03 20:04

    • first compares the two Type objects (types are themselves object in .net)
    • second, if well written (myObj is AClass) check compatibility between two types. if myObj is an instance of a class inheriting from AClass, it will return true.

    typeof(T) is AClass returns false because typeof(T) is Type and AClass does not inherit from Type

提交回复
热议问题