What is the difference between typeof and the is keyword?

前端 未结 6 1071
太阳男子
太阳男子 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 19:51

    typeof(T) returns a Type instance. and the Type is never equal to AClass

    var t1 = typeof(AClass)); // t1 is a "Type" object
    
    var t2 = new AClass(); // t2 is a "AClass" object
    
    t2 is AClass; // true
    t1 is AClass; // false, because of t1 is a "Type" instance, not a "AClass" instance
    

提交回复
热议问题