Here is a better (and more elegant) example. Use it when you need to check for objets types without any casting:
PS: C# Code
class Program
{
class Parent
{
int a;
}
class Child : Parent
{
int b;
}
static void Main(string[] args)
{
var obj1 = new Parent();
var obj2 = new Child();
Console.WriteLine("obj1 is a Child class Object? {0}", typeof(Child).IsInstanceOfType(obj1));
Console.WriteLine("obj2 is a Child class Object? {0}", typeof(Child).IsInstanceOfType(obj2));
Console.WriteLine("Obj1 is assignable from Child class? {0}", obj1.GetType().IsAssignableFrom(typeof(Child)));
Console.WriteLine("Obj2 is assignable from Parent class? {0}", obj2.GetType().IsAssignableFrom(typeof(Parent)));
Console.ReadLine();
}
}
A little bit explanation of the four checks:
The first one returns false, because it is of Parent type, not Child.
The second returns true, because it is a Child object.
The third returns true, as a Parent variable may receive a Child object, as it is a inherited class (OO fundamentals).
The fourth returns false, because as the Child objectwas inherited from Parent, it is expected that it will have more methods, variables, etc. So a variable of its type will not accept receiving an object of its parent type.
[center]

[center]