Object::GetType Method ()


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Gets the Type of the current instance.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

Type^ GetType()

Return Value

Type: System::Type^

The exact runtime type of the current instance.

Because System::Object is the base class for all types in the .NET Framework type system, the GetType method can be used to return Type objects that represent all .NET Framework types. The .NET Framework recognizes the following five categories of types:

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.

No code example is currently available or this language may not be supported.

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 GetType method is inherited by all types that derive from Object. This means that, in addition to using your own language's comparison keyword, you can use the GetType method to determine the type of a particular object, as the following example shows.

No code example is currently available or this language may not be supported.

The Type object exposes the metadata associated with the class of the current Object.

The following code example demonstrates that GetType returns the runtime type of the current instance.

using namespace System;

public ref class MyBaseClass {};

public ref class MyDerivedClass: 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 


Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top