Windows Dev Center

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.

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 has an element type. An array type, a ref or out parameter, or a constructed ByRef type all have element types.


using System;
using System.Reflection;

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

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // 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();
      outputBlock.Text += String.Format("HasElementType is '{0}' for array types.", t.HasElementType) + "\n";

      // Test an array type without defining an array.
      t = typeof(Example[]);
      outputBlock.Text += String.Format("HasElementType is '{0}' for array types.", t.HasElementType) + "\n";

      // 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();
      outputBlock.Text += String.Format("HasElementType is '{0}' for array types.", t.HasElementType) + "\n";

      // 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;
      outputBlock.Text += String.Format("HasElementType is '{0}' for ref parameter types.", t.HasElementType) + "\n";
      t = parms[1].ParameterType;
      outputBlock.Text += String.Format("HasElementType is '{0}' for out parameter types.", t.HasElementType) + "\n";

      // 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();
      outputBlock.Text += String.Format("HasElementType is '{0}' for pointer types.", t.HasElementType) + "\n";
      t = typeof(Example).MakeByRefType();
      outputBlock.Text += String.Format("HasElementType is '{0}' for ByRef types.", t.HasElementType) + "\n";
   }
}

/* This example produces the following output:

HasElementType is 'True' for array types.
HasElementType is 'True' for array types.
HasElementType is 'True' for array types.
HasElementType is 'True' for ref parameter types.
HasElementType is 'True' for out parameter types.
HasElementType is 'True' for ByRef types.
 */


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2015 Microsoft