Answers for "c# 6 check if object type is"

C#
2

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
0

c# check if object is of any generic type

return list.GetType().GetGenericTypeDefinition() == typeof(List<>);
Posted by: Guest on September-18-2020

Code answers related to "c# 6 check if object type is"

C# Answers by Framework

Browse Popular Code Answers by Language