Object::GetType Method
Updated: December 2010
Gets the type of the current instance.
Assembly: mscorlib (in mscorlib.dll)
For two objects x and y that have identical runtime types, Object.ReferenceEquals(x.GetType(),y.GetType()) returns true. The following example uses the GetType method with the ReferenceEquals method to determine whether one numeric value is the same type as two other numeric values.
![]() |
---|
To determine whether an object is a specific type, you can use your language's type comparison keyword or construct. For example, you can use the TypeOf…Is construct in Visual Basic or the is keyword in C#. |
The Type object exposes the metadata associated with the class of the current Object.
The following example demonstrates that GetType returns the runtime type of the current instance.
using namespace System; public ref class MyBaseClass: public Object{}; public ref class MyDerivedClass: public MyBaseClass{}; int main() { MyBaseClass^ myBase = gcnew MyBaseClass; MyDerivedClass^ myDerived = gcnew 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 */
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.