Object.GetType Method

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Gets the Type of the current instance.

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

Syntax

Public Function GetType As Type
public Type GetType()

Return Value

Type: System..::.Type
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.

Examples

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


' Example base class and derived class. Note that it
' is not necessary to derive from Object explicitly;
' a class with no Inherits statement implicitly 
' derives from Object.
'
Public Class MyBaseClass
   Inherits Object
End Class

Public Class MyDerivedClass
   Inherits MyBaseClass
End Class

Public Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      Dim base As New MyBaseClass()
      Dim derived As New MyDerivedClass()
      Dim o As Object = derived
      Dim b As MyBaseClass = derived

      outputBlock.Text += String.Format("base.GetType returns {0}", base.GetType()) & vbCrLf
      outputBlock.Text += String.Format("derived.GetType returns {0}", derived.GetType()) & vbCrLf
      outputBlock.Text += String.Format("Dim o As Object = derived; o.GetType returns {0}", o.GetType()) & vbCrLf
      outputBlock.Text += String.Format("Dim b As MyBaseClass = derived; b.Type returns {0}", b.GetType()) & vbCrLf

   End Sub
End Class

'This code example produces the following output:
'
'base.GetType returns MyBaseClass
'derived.GetType returns MyDerivedClass
'Dim o As Object = derived; o.GetType returns MyDerivedClass
'Dim b As MyBaseClass = derived; b.Type returns MyDerivedClass
'
using System;

public class MyBaseClass : Object
{
}

public class MyDerivedClass : MyBaseClass
{
}

public class Example
{

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      MyBaseClass myBase = new MyBaseClass();
      MyDerivedClass myDerived = new MyDerivedClass();
      object o = myDerived;
      MyBaseClass b = myDerived;

      outputBlock.Text += String.Format("mybase: Type is {0}", myBase.GetType()) + "\n";
      outputBlock.Text += String.Format("myDerived: Type is {0}", myDerived.GetType()) + "\n";
      outputBlock.Text += String.Format("object o = myDerived: Type is {0}", o.GetType()) + "\n";
      outputBlock.Text += String.Format("MyBaseClass b = myDerived: Type is {0}", b.GetType()) + "\n";
   }
}


/*

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 

*/

Version Information

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Platforms

Windows Phone

See Also

Reference

Object Class

System Namespace

Type