Export (0) Print
Expand All

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.

[Visual Basic]
Public ReadOnly Property HasElementType As Boolean
[C#]
public bool HasElementType {get;}
[C++]
public: __property bool get_HasElementType();
[JScript]
public function get HasElementType() : Boolean;

Property Value

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

Remarks

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

Example

[Visual Basic, C#, C++] The following example returns true or false depending on whether or not the object is an array, a reference type, or a pointer.

[Visual Basic] 
Imports System
Imports Microsoft.VisualBasic
Public Class [MyClass]
End Class '[MyClass]
Public Class Type_HasElementType
    Public Shared Sub Main()
        Try
            ' Define a nonarray, pointer, or reference element. 
            Dim myObject1 As New [MyClass]()
            Dim myObject2 As [Object] = myObject1
            ' An object of array type. 
            Dim myObject3(4) As [MyClass]
            Dim myObject4 As [Object] = myObject3
            Dim myObjects() As [Object] = {myObject1, myObject2, myObject3, myObject4}
            Console.WriteLine(ControlChars.NewLine + "Check whether the object is an array, pointer, or reference type." + ControlChars.NewLine)
            Dim i As Integer
            For i = 0 To myObjects.Length - 1
                If myObjects(i).GetType().HasElementType Then
                    Console.WriteLine("myObject{0} is an array, pointer, or reference type.", i.ToString())
                Else
                    Console.WriteLine("myObject{0} is not an array, pointer, or reference type.", i.ToString())
                End If
            Next i
        Catch e As Exception
            Console.WriteLine("Exception: {0} " + ControlChars.Cr, e.Message.ToString())
        End Try
    End Sub 'Main 
End Class 'Type_HasElementType

[C#] 
using System;
public class MyClass 
{
}
public class Type_HasElementType
{
    public static void Main()
    {
        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.Length; i++)
            {
                if(myObjects[i].GetType().HasElementType)
                    Console.WriteLine("myObject{0} is an array, pointer, or reference type.", i);
                else
                    Console.WriteLine("myObject{0} is not an array, pointer, or reference type.", i);
            }
        }
        catch( Exception e )
        {
            Console.WriteLine( "Exception: {0} \n", e.Message ) ; 
        }    
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
public __gc class MyClass {
};
int main() {
   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[] ={ myObject1, myObject2, myObject3, myObject4 };
      Console::WriteLine(S"\nCheck whether the object is an array, a pointer, or a reference type.\n");
      for (int i = 0; i < myObjects->Length; i++) {
         if (myObjects->Item[i]->GetType()->HasElementType)
            Console::WriteLine(S"myObject{0} is an array, pointer, or reference type.", __box(i));
         else
            Console::WriteLine(S"myObject{0} is not an array, pointer, or reference type.", __box(i));
      }
   } catch (Exception* e) {
      Console::WriteLine(S"Exception: {0} \n", e->Message) ;
   }
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Type Class | Type Members | System Namespace | HasElementTypeImpl | IsArray | IsPointer | IsByRef | GetElementType | GetType

Show:
© 2014 Microsoft