This documentation is archived and is not being maintained.

Type.HasElementType Property

Gets a value indicating whether the current Type encompasses or refers to another type; that is, whether the current Type is an array, a pointer, or is passed by reference.

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

'Declaration
Public ReadOnly Property HasElementType As Boolean
'Usage
Dim instance As Type
Dim value As Boolean

value = instance.HasElementType

/** @property */
public final boolean get_HasElementType ()

public final function get HasElementType () : boolean

Not applicable.

Property Value

true if the Type is an array, a pointer, or is passed by reference; otherwise, false.

For example, Type.GetType("Int32[]").HasElementType returns true, but Type.GetType("Int32").HasElementType returns false. HasElementType also returns true for "Int32*" and "Int32&".

If the current Type represents a generic type, or a type parameter in the definition of a generic type or generic method, this property always returns false.

The following example returns true or false depending on whether or not the object is an array, a reference type, or a pointer.

Imports System
Imports System.Reflection
Imports System.Runtime.InteropServices

Public Class Example

    ' This method is for demonstration purposes.
    Public Shared Sub Test(ByRef x As Integer, <Out> ByRef y As Integer)
    End Sub

    Public Shared Sub Main()
        ' All of the following display 'True'.

        ' Define an array, get its type, and display HasElementType. 
        Dim nums() As Integer = {1, 1, 2, 3, 5, 8, 13}
        Dim t As Type = nums.GetType()
        Console.WriteLine("HasElementType is '{0}' for array types.", t.HasElementType)

        ' Test an array type without defining an array.
        t = GetType(Example())
        Console.WriteLine("HasElementType is '{0}' for array types.", t.HasElementType)

        ' When you use Reflection Emit to emit dynamic methods and
        ' assemblies, you can create array types using MakeArrayType.
        ' The following creates the type 'array of Example'.
        t = GetType(Example).MakeArrayType()
        Console.WriteLine("HasElementType is '{0}' for array types.", t.HasElementType)

        ' When you reflect over methods, HasElementType is true for
        ' ref, out, and pointer parameter types. The following 
        ' gets the Test method, defined above, and examines its
        ' parameters.
        Dim mi As MethodInfo = GetType(Example).GetMethod("Test")
        Dim parms() As ParameterInfo = mi.GetParameters()
        t = parms(0).ParameterType
        Console.WriteLine("HasElementType is '{0}' for ref parameter types.", t.HasElementType)
        t = parms(1).ParameterType
        Console.WriteLine("HasElementType is '{0}' for <Out> parameter types.", t.HasElementType)

        ' When you use Reflection Emit to emit dynamic methods and
        ' assemblies, you can create pointer and ByRef types to use
        ' when you define method parameters.
        t = GetType(Example).MakePointerType()
        Console.WriteLine("HasElementType is '{0}' for pointer types.", t.HasElementType)
        t = GetType(Example).MakeByRefType()
        Console.WriteLine("HasElementType is '{0}' for ByRef types.", t.HasElementType)
    End Sub 

End Class

import System.*;

public class MyClass
{
} //MyClass

public class Type_HasElementType
{
    public static void main(String[] args)
    {
        try {
            // A nonarray, pointer, or reference element. 
            MyClass myObject1 = new MyClass();
            Object myObject2 = myObject1;

            // Define an array. 
            MyClass myObject3[] = new MyClass[5];
            Object myObject4 = myObject3;
            Object myObjects[] = new Object[] { myObject1, myObject2,
                myObject3, myObject4 };
            Console.WriteLine("\nCheck whether the object is an array, "
                +"a pointer, or a reference type.\n");
            for (int i = 0; i < myObjects.get_Length(); i++) {
                if (myObjects.get_Item(i).GetType().get_HasElementType()) {
                    Console.WriteLine("myObject{0} is an array, pointer, or "
                        +"reference type.", System.Convert.ToString(i));
                }
                else {
                    Console.WriteLine("myObject{0} is not an array, pointer, " 
                        +"or reference type.", System.Convert.ToString(i));
                }
            }
        }
        catch (System.Exception e) {
            Console.WriteLine("Exception: {0} \n", e.get_Message());
        }
    } //main
} //Type_HasElementType

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
Show: