Type.MakeArrayType Method (Int32)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Updated: June 2010

Returns a Type object representing an array of the current type, with the specified number of dimensions.

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

Syntax

'Declaration
Public Overridable Function MakeArrayType ( _
    rank As Integer _
) As Type
public virtual Type MakeArrayType(
    int rank
)

Parameters

  • rank
    Type: System.Int32
    The number of dimensions for the array. This number must be less than or equal to 32.

Return Value

Type: System.Type
A Type object representing an array of the current type, with the specified number of dimensions.

Exceptions

Exception Condition
IndexOutOfRangeException

rank is invalid. For example, 0 or negative.

NotSupportedException

The invoked method is not supported in the base class.

TypeLoadException

The current type is TypedReference.

-or-

The current type is a ByRef type. That is, Type.IsByRef returns true.

-or-

rank is greater than 32.

Remarks

The MakeArrayType method provides a way to generate array types whose element types are computed at run time.

NoteNote:

The common language runtime makes a distinction between vectors (that is, one-dimensional arrays that are always zero-based) and multidimensional arrays. A vector, which always has only one dimension, is not the same as a multidimensional array that happens to have only one dimension. You cannot use this method overload to create a vector type; if rank is 1, this method overload returns a multidimensional array type that happens to have one dimension. Use the MakeArrayType() method overload to create vector types.

Examples

The following code example creates array, ref (ByRef in Visual Basic), and pointer types for the Test class.

Imports System.Reflection

Public Class Example
   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      ' Create a Type object that represents a one-dimensional
      ' array of Example objects.
      Dim t As Type = GetType(Example).MakeArrayType()
      outputBlock.Text &= vbCrLf & "Array of Example: " & t.ToString() & vbCrLf

      ' Create a Type object that represents a two-dimensional
      ' array of Example objects.
      t = GetType(Example).MakeArrayType(2)
      outputBlock.Text &= vbCrLf & "Two-dimensional array of Example: " & t.ToString() & vbCrLf

      ' Demonstrate an exception when an invalid array rank is
      ' specified.
      Try
         t = GetType(Example).MakeArrayType(-1)
      Catch ex As Exception
         outputBlock.Text &= vbCrLf & ex.ToString() & vbCrLf
      End Try

      ' Create a Type object that represents a ByRef parameter
      ' of type Example.
      t = GetType(Example).MakeByRefType()
      outputBlock.Text &= vbCrLf & "ByRef Example: " & t.ToString() & vbCrLf

      ' Get a Type object representing the Example class, a
      ' MethodInfo representing the "Test" method, a ParameterInfo
      ' representing the parameter of type Example, and finally
      ' a Type object representing the type of this ByRef parameter.
      ' Compare this Type object with the Type object created using
      ' MakeByRefType.
      Dim t2 As Type = GetType(Example)
      Dim mi As MethodInfo = t2.GetMethod("Test")
      Dim pi As ParameterInfo = mi.GetParameters()(0)
      Dim pt As Type = pi.ParameterType
      outputBlock.Text &= "Are the ByRef types equal? " & (t Is pt) & vbCrLf

      ' Create a Type object that represents a pointer to an
      ' Example object.
      t = GetType(Example).MakePointerType()
      outputBlock.Text &= vbCrLf & "Pointer to Example: " & t.ToString() & vbCrLf
   End Sub

   ' A sample method with a ByRef parameter.
   '
   Public Sub Test(ByRef e As Example)
   End Sub
End Class

' This example produces output similar to the following:
'
'Array of Example: SilverlightApplication.Example[]
'
'Two-dimensional array of Example: SilverlightApplication.Example[,]
'
'System.IndexOutOfRangeException: Index was outside the bounds of the array.
'   at System.RuntimeType.MakeArrayType(Int32 rank) in c:\vbl\ndp\clr\src\BCL\System\RtType.cs:line 2999
'   at SilverlightApplication.Example.Demo(TextBlock outputBlock)
'
'ByRef Example: SilverlightApplication.Example&
'Are the ByRef types equal? True
'
'Pointer to Example: SilverlightApplication.Example*
using System;
using System.Reflection;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // Create a Type object that represents a one-dimensional
      // array of Example objects.
      Type t = typeof(Example).MakeArrayType();
      outputBlock.Text += String.Format("\r\nArray of Example: {0}\n", t);

      // Create a Type object that represents a two-dimensional
      // array of Example objects.
      t = typeof(Example).MakeArrayType(2);
      outputBlock.Text += String.Format("\r\nTwo-dimensional array of Example: {0}\n", t);

      // Demonstrate an exception when an invalid array rank is
      // specified.
      try
      {
         t = typeof(Example).MakeArrayType(-1);
      }
      catch (Exception ex)
      {
         outputBlock.Text += String.Format("\r\n{0}\n", ex);
      }

      // Create a Type object that represents a ByRef parameter
      // of type Example.
      t = typeof(Example).MakeByRefType();
      outputBlock.Text += String.Format("\r\nByRef Example: {0}\n", t);

      // Get a Type object representing the Example class, a
      // MethodInfo representing the "Test" method, a ParameterInfo
      // representing the parameter of type Example, and finally
      // a Type object representing the type of this ByRef parameter.
      // Compare this Type object with the Type object created using
      // MakeByRefType.
      Type t2 = typeof(Example);
      MethodInfo mi = t2.GetMethod("Test");
      ParameterInfo pi = mi.GetParameters()[0];
      Type pt = pi.ParameterType;
      outputBlock.Text += String.Format("Are the ByRef types equal? {0}\n", (t == pt));

      // Create a Type object that represents a pointer to an
      // Example object.
      t = typeof(Example).MakePointerType();
      outputBlock.Text += String.Format("\r\nPointer to Example: {0}\n", t);
   }

   // A sample method with a ByRef parameter.
   //
   public void Test(ref Example e)
   {
   }
}

/* This example produces output similar to the following:

Array of Example: Example[]

Two-dimensional array of Example: Example[,]

System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at System.RuntimeType.MakeArrayType(Int32 rank) in c:\vbl\ndp\clr\src\BCL\System\RtType.cs:line 2999
   at Example.Demo(TextBlock outputBlock)

ByRef Example: Example&
Are the ByRef types equal? True

Pointer to Example: Example*

 */

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Change History

Date

History

Reason

June 2010

Added missing TypeLoadException exception.

Customer feedback.