Export (0) Print
Expand All
Expand Minimize
This topic has not yet been rated - Rate this topic

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)

public abstract Type GetElementType ()
public abstract Type GetElementType ()
public abstract function GetElementType () : Type
Not applicable.

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.

using System;
class TestGetElementType 
{
    public static void Main() 
    {
        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());
    }
}

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 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
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.