📜  如何检查这种类型的对象c#代码示例

📅  最后修改于: 2022-03-11 14:48:45.253000             🧑  作者: Mango

代码示例1
class Animal { } 
class Dog : Animal { }

void PrintTypes(Animal a) { 
    Console.WriteLine(a.GetType() == typeof(Animal)); // false 
    Console.WriteLine(a is Animal);                   // true 
    Console.WriteLine(a.GetType() == typeof(Dog));    // true
    Console.WriteLine(a is Dog);                      // true 
}

Dog spot = new Dog(); 
PrintTypes(spot);
//Summary GetType more strict in contrast with "is", that even with parents OK too