Поделиться через


Array.CreateInstance Method (Type, Int32)

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

Creates a one-dimensional Array of the specified Type and length, with zero-based indexing.

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

Syntax

'Declaration
<SecuritySafeCriticalAttribute> _
Public Shared Function CreateInstance ( _
    elementType As Type, _
    length As Integer _
) As Array
[SecuritySafeCriticalAttribute]
public static Array CreateInstance(
    Type elementType,
    int length
)

Parameters

Return Value

Type: System.Array
A new one-dimensional Array of the specified Type with the specified length, using zero-based indexing.

Exceptions

Exception Condition
ArgumentNullException

elementType is nulla null reference (Nothing in Visual Basic).

ArgumentException

elementType is not a valid Type.

NotSupportedException

elementType is not supported. For example, Void is not supported.

-or-

elementType is an open generic type.

ArgumentOutOfRangeException

length is less than zero.

Remarks

Unlike most classes, Array provides the CreateInstance method, instead of public constructors, to allow for late bound access.

Reference-type elements are initialized to nulla null reference (Nothing in Visual Basic). Value-type elements are initialized to zero.

This method is an O(n) operation, where n is length.

Examples

The following code example shows how to create and initialize a one-dimensional Array.


Public Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      ' Creates and initializes a one-dimensional Array of type Int32.
      Dim my1DArray As Array = Array.CreateInstance(GetType(Int32), 5)
      Dim i As Integer
      For i = my1DArray.GetLowerBound(0) To my1DArray.GetUpperBound(0)
         my1DArray.SetValue(i + 1, i)
      Next i
      ' Displays the values of the Array.
      outputBlock.Text &= "The one-dimensional Array contains the " _
         + "following values:" & vbCrLf
      PrintValues(outputBlock, my1DArray)

   End Sub

   Public Shared Sub PrintValues(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal myArr As Array)
      Dim myEnumerator As System.Collections.IEnumerator = _
         myArr.GetEnumerator()
      Dim i As Integer = 0
      Dim cols As Integer = myArr.GetLength((myArr.Rank - 1))
      While myEnumerator.MoveNext()
         If i < cols Then
            i += 1
         Else
            outputBlock.Text &= vbCrLf
            i = 1
         End If
         outputBlock.Text &= String.Format(ControlChars.Tab + "{0}", myEnumerator.Current)
      End While
      outputBlock.Text &= vbCrLf
   End Sub
End Class

' This code produces the following output.
' 
' The one-dimensional Array contains the following values:
'     1    2    3    4    5 
using System;
public class Example
{

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {

      // Creates and initializes a one-dimensional Array of type Int32.
      Array my1DArray = Array.CreateInstance(typeof(Int32), 5);
      for (int i = my1DArray.GetLowerBound(0); i <= my1DArray.GetUpperBound(0); i++)
         my1DArray.SetValue(i + 1, i);

      // Displays the values of the Array.
      outputBlock.Text += "The one-dimensional Array contains the following values:" + "\n";
      PrintValues(outputBlock, my1DArray);
   }


   public static void PrintValues(System.Windows.Controls.TextBlock outputBlock, Array myArr)
   {
      System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();
      int i = 0;
      int cols = myArr.GetLength(myArr.Rank - 1);
      while (myEnumerator.MoveNext())
      {
         if (i < cols)
         {
            i++;
         }
         else
         {
            outputBlock.Text += "\n";
            i = 1;
         }
         outputBlock.Text += String.Format("\t{0}", myEnumerator.Current);
      }
      outputBlock.Text += "\n";
   }
}
/*
This code produces the following output.

The one-dimensional Array contains the following values:
    1    2    3    4    5
*/

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.