Object.GetType Method
Gets the Type of the current instance.
[Visual Basic] Public Function GetType() As Type [C#] public Type GetType(); [C++] public: Type* GetType(); [JScript] public function GetType() : Type;
Return Value
The Type instance that represents the exact runtime type of the current instance.
Remarks
For two objects x and y that have identical runtime types, Object.ReferenceEquals(x.GetType(),y.GetType()) returns true.
The Type object exposes the metadata associated with the class of the current Object.
Example
[C#, C++, JScript] The following code example demonstrates that GetType returns the runtime type of the current instance.
[C#] 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()); } } /* This code produces the following output. mybase: Type is MyBaseClass myDerived: Type is MyDerivedClass object o = myDerived: Type is MyDerivedClass MyBaseClass b = myDerived: Type is MyDerivedClass */ [C++] #using <mscorlib.dll> using namespace System; public __gc class MyBaseClass: public Object { }; public __gc class MyDerivedClass: public MyBaseClass { }; int main() { MyBaseClass* myBase = new MyBaseClass(); MyDerivedClass* myDerived = new MyDerivedClass(); Object* o = myDerived; MyBaseClass* b = myDerived; Console::WriteLine(S"mybase: Type is {0}", myBase->GetType()); Console::WriteLine(S"myDerived: Type is {0}", myDerived->GetType()); Console::WriteLine(S"object o = myDerived: Type is {0}", o->GetType()); Console::WriteLine(S"MyBaseClass b = myDerived: Type is {0}", b->GetType()); } /* This code produces the following output. mybase: Type is MyBaseClass myDerived: Type is MyDerivedClass object o = myDerived: Type is MyDerivedClass MyBaseClass b = myDerived: Type is MyDerivedClass */ [JScript] import System public class MyBaseClass extends Object { } public class MyDerivedClass extends MyBaseClass { } public class Test { public static function Main() { var myBase : MyBaseClass = new MyBaseClass(); var myDerived : MyDerivedClass = new MyDerivedClass(); var o = myDerived; var b : MyBaseClass = 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()); } } Test.Main(); /* This code produces the following output. mybase: Type is MyBaseClass myDerived: Type is MyDerivedClass object o = myDerived: Type is MyDerivedClass MyBaseClass b = myDerived: Type is MyDerivedClass */
[Visual Basic] No example is available for Visual Basic. To view a C#, C++, or JScript example, click the Language Filter button in the upper-left corner of the page.
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard