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

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)
public bool HasElementType { get; }

Property Value

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

Implements

_Type.HasElementType

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.

// This code must be compiled with the /unsafe switch: 
//   csc /unsafe source.cs 
using System;
using System.Reflection;

public class Example
{
    // This method is for demonstration purposes. 
    unsafe public void Test(ref int x, out int y, int* z) 
    { 
        *z = x = y = 0; 
    }

    public static void Main()
    {
        // All of the following display 'True'. 

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

        // Test an array type without defining an array.
        t = typeof(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 = typeof(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.
        MethodInfo mi = typeof(Example).GetMethod("Test");
        ParameterInfo[] parms = 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);
        t = parms[2].ParameterType;
        Console.WriteLine("HasElementType is '{0}' for pointer 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 = typeof(Example).MakePointerType();
        Console.WriteLine("HasElementType is '{0}' for pointer types.", t.HasElementType);
        t = typeof(Example).MakeByRefType();
        Console.WriteLine("HasElementType is '{0}' for ByRef types.", t.HasElementType);
    }
}

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.