Mono Class Library: System.Object Overview | Members

System.Object.GetType Method

Gets the type of the current instance.

public Type GetType ()

Returns

The instance of Type that represents the run-time type (the exact type) of the current instance.

Remarks

For two objects x and y that have identical run-time types, object.ReferenceEquals(object, object)(x.GetType(),y.GetType()) returns true .

Example

The following example demonstrates the fact that object.GetType returns the run-time type of the current instance:

C# Example

using System;
public class MyBaseClass: Object {
}
public class MyDerivedClass: MyBaseClass {
}
public class Test {
   public static void Main() {
   MyBaseClass myBase = new MyBaseClass();
   MyDerivedClass myDerived = new MyDerivedClass();

   object o = myDerived;
   MyBaseClass b = myDerived;

   Console.WriteLine("mybase: Type is {0}", myBase.GetType());
   Console.WriteLine("myDerived: Type is {0}", myDerived.GetType());
   Console.WriteLine("object o = myDerived: Type is {0}", o.GetType());
   Console.WriteLine("MyBaseClass b = myDerived: Type is {0}", b.GetType());
   }
}

The output is

mybase: Type is MyBaseClass
myDerived: Type is MyDerivedClass
object o = myDerived: Type is MyDerivedClass
MyBaseClass b = myDerived: Type is MyDerivedClass

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0