Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplest way to determine if class x is derived from class y? (c#)

Is this the simplest way to determine if foo is the same or derived from type T

bool Derives<T>(object foo)
{
  return foo is T;
}

and an exact match would be

bool ExactMatch<T>(object foo)
{
  return foo.GetType() == typeof(T);
}
like image 915
maxp Avatar asked May 10 '11 10:05

maxp


People also ask

How do you check if an object is an instance of a class C++?

C++ has no direct method to check one object is an instance of some class type or not. In Java, we can get this kind of facility. In C++11, we can find one item called is_base_of<Base, T>. This will check if the given class is a base of the given object or not.

Which keyword is used to identify a derived class?

The extends keyword in the Child class definition tells you it's a derived class.

How do you identify a base class and a derived class?

1. A base class is an existing class from which the other classes are derived and inherit the methods and properties. A derived class is a class that is constructed from a base class or an existing class.

How do you derive a class from another class in C++?

Inheritance in C++ The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important features of Object-Oriented Programming. Inheritance is a feature or a process in which, new classes are created from the existing classes.


1 Answers

I can't think of a simpler way :)

(and in 'answer' format, to please the trolls: "Yes")

like image 163
Antony Woods Avatar answered Oct 13 '22 01:10

Antony Woods