Home

Published

- 1 min read

how to check is object by this type c#

img of how to check is object by this type c#

The solution for this is noted below

how to check is object by this type c#

Solution

   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

Try other methods by searching on the site. That is if this doesn’t work