Object.GetType Method

Gets the Type of the current instance.

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

public Type GetType()

Return Value

Type: System.Type
The exact runtime type of the current instance.

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.

int n1 = 12;
int n2 = 82;
long n3 = 12;

Console.WriteLine("n1 and n2 are the same type: {0}",
                  Object.ReferenceEquals(n1.GetType(), n2.GetType()));
Console.WriteLine("n1 and n3 are the same type: {0}",
                  Object.ReferenceEquals(n1.GetType(), n3.GetType()));
// The example displays the following output: 
//       n1 and n2 are the same type: True 
//       n1 and n3 are the same type: False      
NoteNote

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 code example demonstrates that GetType returns the runtime type of the current instance.

using System;

public class MyBaseClass {
}

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 example displays the following output: 
//    mybase: Type is MyBaseClass 
//    myDerived: Type is MyDerivedClass 
//    object o = myDerived: Type is MyDerivedClass 
//    MyBaseClass b = myDerived: Type is MyDerivedClass 

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft