Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Array Class

Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the base class for all arrays in the common language runtime.

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

[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public abstract class Array : ICloneable, IList, ICollection, 
	IEnumerable
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public abstract class Array implements ICloneable, IList, 
	ICollection, IEnumerable
SerializableAttribute 
ComVisibleAttribute(true) 
public abstract class Array implements ICloneable, IList, 
	ICollection, IEnumerable

The Array class is the base class for language implementations that support arrays. However, only the system and compilers can derive explicitly from the Array class. Users should employ the array constructs provided by the language.

An element is a value in an Array. The length of an Array is the total number of elements it can contain. The rank of an Array is the number of dimensions in the Array. The lower bound of a dimension of an Array is the starting index of that dimension of the Array; a multidimensional Array can have different bounds for each dimension.

NoteImportant:

In the .NET Framework version 2.0, the Array class implements the System.Collections.Generic.IList, System.Collections.Generic.ICollection, and System.Collections.Generic.IEnumerable generic interfaces. The implementations are provided to arrays at run time, and therefore are not visible to the documentation build tools. As a result, the generic interfaces do not appear in the declaration syntax for the Array class, and there are no reference topics for interface members that are accessible only by casting an array to the generic interface type (explicit interface implementations). The key thing to be aware of when you cast an array to one of these interfaces is that members which add, insert, or remove elements throw NotSupportedException.

Type objects provide information about array type declarations. Array objects with the same array type share the same Type object.

Type.IsArray and Type.GetElementType might not return the expected results with Array because if an array is cast to the type Array, the result is an object, not an array. That is, typeof(System.Array).IsArray returns false, and typeof(System.Array).GetElementType returns a null reference (Nothing in Visual Basic).

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

The Array.Copy method copies elements not only between arrays of the same type but also between standard arrays of different types; it handles type casting automatically.

Some methods, such as CreateInstance, Copy, CopyTo, GetValue, and SetValue, provide overloads that accept 64-bit integers as parameters to accommodate large capacity arrays. LongLength and GetLongLength return 64-bit integers indicating the length of the array.

The Array is not guaranteed to be sorted. You must sort the Array prior to performing operations (such as BinarySearch) that require the Array to be sorted.

The following code example shows how Array.Copy copies elements between an array of type integer and an array of type Object.

public class SamplesArray  {

   public static void Main()  {

      // Creates and initializes a new integer array and a new Object array.
      int[] myIntArray = new int[5] { 1, 2, 3, 4, 5 };
      Object[] myObjArray = new Object[5] { 26, 27, 28, 29, 30 };

      // Prints the initial values of both arrays.
      Console.WriteLine( "Initially," );
      Console.Write( "integer array:" );
      PrintValues( myIntArray );
      Console.Write( "Object array: " );
      PrintValues( myObjArray );

      // Copies the first two elements from the integer array to the Object array.
      Array.Copy( myIntArray, myObjArray, 2 );

      // Prints the values of the modified arrays.
      Console.WriteLine( "\nAfter copying the first two elements of the integer array to the Object array," );
      Console.Write( "integer array:" );
      PrintValues( myIntArray );
      Console.Write( "Object array: " );
      PrintValues( myObjArray );

      // Copies the last two elements from the Object array to the integer array.
      Array.Copy( myObjArray, myObjArray.GetUpperBound(0) - 1, myIntArray, myIntArray.GetUpperBound(0) - 1, 2 );

      // Prints the values of the modified arrays.
      Console.WriteLine( "\nAfter copying the last two elements of the Object array to the integer array," );
      Console.Write( "integer array:" );
      PrintValues( myIntArray );
      Console.Write( "Object array: " );
      PrintValues( myObjArray );
   }


   public static void PrintValues( Object[] myArr )  {
      foreach ( Object i in myArr )  {
         Console.Write( "\t{0}", i );
      }
      Console.WriteLine();
   }

   public static void PrintValues( int[] myArr )  {
      foreach ( int i in myArr )  {
         Console.Write( "\t{0}", i );
      }
      Console.WriteLine();
   }
}
/* 
This code produces the following output.

Initially,
integer array:  1       2       3       4       5
Object array:   26      27      28      29      30

After copying the first two elements of the integer array to the Object array,
integer array:  1       2       3       4       5
Object array:   1       2       28      29      30

After copying the last two elements of the Object array to the integer array,
integer array:  1       2       3       29      30
Object array:   1       2       28      29      30
*/

public class SamplesArray
{
    public static void main(String[] args)
    {
        // Creates and initializes a new integer array and a new Object array.
        int myIntArray[] = new int[] { 1, 2, 3, 4, 5 };
        Object myObjArray[] = new Object[] { (Int32)26, (Int32)27, (Int32)28, 
            (Int32)29, (Int32)30};
        // Prints the initial values of both arrays.
        Console.WriteLine("Initially,");
        Console.Write("integer array:");
        PrintValues(myIntArray);
        Console.Write("Object array: ");
        PrintValues(myObjArray);
        // Copies the first two elements from the integer array to the 
        // Object array.
        Array.Copy(myIntArray, myObjArray, 2);
        // Prints the values of the modified arrays.
        Console.WriteLine("\nAfter copying the first two elements of the"
            + " integer array to the Object array,");
        Console.Write("integer array:");
        PrintValues(myIntArray);
        Console.Write("Object array: ");
        PrintValues(myObjArray);
        // Copies the last two elements from the Object array to the
        // integer array.
        Array.Copy(myObjArray, myObjArray.GetUpperBound(0) - 1, myIntArray, 
            myIntArray.GetUpperBound(0) - 1, 2);
        // Prints the values of the modified arrays.
        Console.WriteLine("\nAfter copying the last two elements of the Object"
            + " array to the integer array,");
        Console.Write("integer array:");
        PrintValues(myIntArray);
        Console.Write("Object array: ");
        PrintValues(myObjArray);
    } //main

    public static void PrintValues(Object myArr[])
    {
        Object i = null;
        for (int iCtr = 0; iCtr < myArr.get_Length(); iCtr++) {
            i = myArr[iCtr];
            Console.Write("\t{0}", i);
        }
        Console.WriteLine();
    } //PrintValues

    public static void PrintValues(int myArr[])
    { 
        int i = 0;
        for (int iCtr = 0; iCtr < myArr.get_Length(); iCtr++) {
            i = myArr[iCtr];
            Console.Write("\t{0}", (Int32)i);
        }
        Console.WriteLine();
    } //PrintValues
} //SamplesArray

/* 
 This code produces the following output.
 
 Initially,
 integer array:  1       2       3       4       5
 Object array:   26      27      28      29      30

 After copying the first two elements of the integer array to the Object array,
 integer array:  1       2       3       4       5
 Object array:   1       2       28      29      30

 After copying the last two elements of the Object array to the integer array,
 integer array:  1       2       3       29      30
 Object array:   1       2       28      29      30
 */

The following code example creates and initializes an Array and displays its properties and its elements.

public class SamplesArray2{

   public static void Main()  {

      // Creates and initializes a new three-dimensional Array of type Int32.
      Array myArr = Array.CreateInstance( typeof(Int32), 2, 3, 4 );
      for ( int i = myArr.GetLowerBound(0); i <= myArr.GetUpperBound(0); i++ )
         for ( int j = myArr.GetLowerBound(1); j <= myArr.GetUpperBound(1); j++ )
            for ( int k = myArr.GetLowerBound(2); k <= myArr.GetUpperBound(2); k++ )  {
               myArr.SetValue( (i*100)+(j*10)+k, i, j, k );
            }

      // Displays the properties of the Array.
      Console.WriteLine( "The Array has {0} dimension(s) and a total of {1} elements.", myArr.Rank, myArr.Length );
      Console.WriteLine( "\tLength\tLower\tUpper" );
      for ( int i = 0; i < myArr.Rank; i++ )  {
         Console.Write( "{0}:\t{1}", i, myArr.GetLength(i) );
         Console.WriteLine( "\t{0}\t{1}", myArr.GetLowerBound(i), myArr.GetUpperBound(i) );
      }

      // Displays the contents of the Array.
      Console.WriteLine( "The Array contains the following values:" );
      PrintValues( myArr );
   }


   public static void PrintValues( 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  {
            Console.WriteLine();
            i = 1;
         }
         Console.Write( "\t{0}", myEnumerator.Current );
      }
      Console.WriteLine();
   }
}
/* 
This code produces the following output.

The Array has 3 dimension(s) and a total of 24 elements.
    Length    Lower    Upper
0:    2    0    1
1:    3    0    2
2:    4    0    3
The Array contains the following values:
    0    1    2    3
    10    11    12    13
    20    21    22    23
    100    101    102    103
    110    111    112    113
    120    121    122    123
*/

public class SamplesArray2
{
    public static void main(String[] args)
    {
        // Creates and initializes a new three-dimensional Array of type Int32.
        Array myArr = Array.CreateInstance(Int32.class.ToType(), 2, 3, 4);
        for (int i = myArr.GetLowerBound(0); i <= myArr.GetUpperBound(0); i++) {
            for (int j = myArr.GetLowerBound(1); j <= myArr.GetUpperBound(1);
                j++) {
                for (int k = myArr.GetLowerBound(2); k <= myArr.
                    GetUpperBound(2); k++) {
                    myArr.SetValue((Int32)(i * 100 + j * 10 + k), i, j, k);
                }
            }
        }  // Displays the properties of the Array.
        Console.WriteLine("The Array has {0} dimension(s) and a total of"
            + " {1} elements.", System.Convert.ToString(myArr.get_Rank()), 
            System.Convert.ToString(myArr.get_Length()));
        Console.WriteLine("\tLength\tLower\tUpper");
        for (int i = 0; i < myArr.get_Rank(); i++) {
            Console.Write("{0}:\t{1}", System.Convert.ToString(i), 
                System.Convert.ToString(myArr.GetLength(i)));
            Console.WriteLine("\t{0}\t{1}", System.Convert.ToString(myArr.
                GetLowerBound(i)), System.Convert.ToString(myArr.
                GetUpperBound(i)));
        }
        // Displays the contents of the Array.
        Console.WriteLine("The Array contains the following values:");
        PrintValues(myArr);
    } //main

    public static void PrintValues(Array myArr)
    {
        System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();
        int i = 0;
        int cols = myArr.GetLength(myArr.get_Rank() - 1);
        while (myEnumerator.MoveNext()) {
            if (i < cols) {
                i++;
            }
            else {
                Console.WriteLine();
                i = 1;
            }
            Console.Write("\t{0}", myEnumerator.get_Current());
        }
        Console.WriteLine();
    } //PrintValues
} //SamplesArray2

/* 
 This code produces the following output.
 
 The Array has 3 dimension(s) and a total of 24 elements.
     Length    Lower    Upper
 0:    2    0    1
 1:    3    0    2
 2:    4    0    3
 The Array contains the following values:
     0    1    2    3
     10    11    12    13
     20    21    22    23
     100    101    102    103
     110    111    112    113
     120    121    122    123
 */

Public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

This implementation does not provide a synchronized (thread safe) wrapper for an Array; however, .NET Framework classes based on Array provide their own synchronized version of the collection using the SyncRoot property.

Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft