Answers for "how to check is object by this type c#"

C#
0

how to check is object by this type c#

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
Posted by: Guest on August-23-2021

Code answers related to "how to check is object by this type c#"

C# Answers by Framework

Browse Popular Code Answers by Language