Type.GetElementType Method

When overridden in a derived class, returns the Type of the object encompassed or referred to by the current array, pointer or reference type.

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

'Declaration
Public MustOverride Function GetElementType As Type
'Usage
Dim instance As Type
Dim returnValue As Type

returnValue = instance.GetElementType
public abstract Type GetElementType ()
public abstract function GetElementType () : Type

Return Value

The Type of the object encompassed or referred to by the current array, pointer, or reference type, or a null reference (Nothing in Visual Basic) if the current Type is not an array or a pointer, or is not passed by reference, or represents a generic type or a type parameter in the definition of a generic type or generic method.

This method returns a null reference (Nothing in Visual Basic) for the Array class.

The following example demonstrates using the GetElementType method.

Imports System

Class TestGetElementType

    Public Shared Sub Main()
        Dim array As Integer() = {1, 2, 3}
        Dim t As Type = array.GetType()
        Dim t2 As Type = t.GetElementType()
        Console.WriteLine("The element type of {0} is {1}.", array, t2.ToString())
        Dim newMe As New TestGetElementType()
        t = newMe.GetType()
        t2 = t.GetElementType()
        If t2 Is Nothing Then
            Console.WriteLine("The element type of {0} is {1}.", newMe, "null")
        Else
            Console.WriteLine("The element type of {0} is {1}.", newMe, t2.ToString())
        End If
    End Sub 'Main
End Class 'TestGetElementType

import System.*;

class TestGetElementType
{
    public static void main(String[] args)
    {
        int array[] =  { 1, 2, 3 };
        Type t = array.GetType();
        Type t2 = t.GetElementType();
        Console.WriteLine("The element type of {0} is {1}.", array,
            t2.ToString());
        TestGetElementType newMe = new TestGetElementType();
        t = newMe.GetType();
        t2 = t.GetElementType();
        Console.WriteLine("The element type of {0} is {1}.", newMe,
            (t2 == null) ? "null" : t2.ToString());
    } //main
} //TestGetElementType

This code produces the following output:

System.Int32[] element type is System.Int32
    TestGetElementType element type is null

Windows 98, Windows 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show: