Export (0) Print
Expand All

Object.GetType Method

Gets the Type of the current instance.

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

'Declaration
Public Function GetType As Type
'Usage
Dim instance As Object
Dim returnValue As Type

returnValue = instance.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.

Imports System

' 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 Test
    
    Public Shared Sub Main() 

        Dim base As New MyBaseClass()
        Dim derived As New MyDerivedClass()
        Dim o As Object = derived
        Dim b As MyBaseClass = derived
        
        Console.WriteLine("base.GetType returns {0}", base.GetType())
        Console.WriteLine("derived.GetType returns {0}", derived.GetType())
        Console.WriteLine("Dim o As Object = derived; o.GetType returns {0}", o.GetType())
        Console.WriteLine("Dim b As MyBaseClass = derived; b.Type returns {0}", b.GetType())
    
    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
'

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