Export (0) Print
Expand All

Object.GetType Method

Gets the Type of the current instance.

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

public Type GetType ()
public Type GetType ()
public function GetType () : Type
Not applicable.

Return Value

The Type instance that represents 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 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: 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 

*/

import System.*;

public class MyBaseClass extends Object
{
} //MyBaseClass

public class MyDerivedClass extends MyBaseClass
{
} //MyDerivedClass

public class Test
{
    public static void main(String[] args)
    {
        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());
    } //main
} //Test

/*

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 

*/

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 

*/

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft