5 out of 16 rated this helpful - Rate this topic

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.

System.Object
  System.Array

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
[SerializableAttribute]
[ComVisibleAttribute(true)]
public abstract class Array : ICloneable, 
	IList, ICollection, IEnumerable, IStructuralComparable, IStructuralEquatable

The Array type exposes the following members.

  Name Description
Public property Supported by the XNA Framework IsFixedSize Gets a value indicating whether the Array has a fixed size.
Public property Supported by the XNA Framework IsReadOnly Gets a value indicating whether the Array is read-only.
Public property Supported by the XNA Framework IsSynchronized Gets a value indicating whether access to the Array is synchronized (thread safe).
Public property Supported by the XNA Framework Supported by Portable Class Library Length Gets a 32-bit integer that represents the total number of elements in all the dimensions of the Array.
Public property LongLength Gets a 64-bit integer that represents the total number of elements in all the dimensions of the Array.
Public property Supported by the XNA Framework Supported by Portable Class Library Rank Gets the rank (number of dimensions) of the Array.
Public property Supported by the XNA Framework SyncRoot Gets an object that can be used to synchronize access to the Array.
Top
  Name Description
Public method Static member AsReadOnly<T> Returns a read-only wrapper for the specified array.
Public method Static member Supported by the XNA Framework BinarySearch(Array, Object) Searches an entire one-dimensional sorted Array for a specific element, using the IComparable interface implemented by each element of the Array and by the specified object.
Public method Static member BinarySearch(Array, Object, IComparer) Searches an entire one-dimensional sorted Array for a value using the specified IComparer interface.
Public method Static member BinarySearch(Array, Int32, Int32, Object) Searches a range of elements in a one-dimensional sorted Array for a value, using the IComparable interface implemented by each element of the Array and by the specified value.
Public method Static member Supported by the XNA Framework BinarySearch(Array, Int32, Int32, Object, IComparer) Searches a range of elements in a one-dimensional sorted Array for a value, using the specified IComparer interface.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library BinarySearch<T>(T[], T) Searches an entire one-dimensional sorted Array for a specific element, using the IComparable<T> generic interface implemented by each element of the Array and by the specified object.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library BinarySearch<T>(T[], T, IComparer<T>) Searches an entire one-dimensional sorted Array for a value using the specified IComparer<T> generic interface.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library BinarySearch<T>(T[], Int32, Int32, T) Searches a range of elements in a one-dimensional sorted Array for a value, using the IComparable<T> generic interface implemented by each element of the Array and by the specified value.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library BinarySearch<T>(T[], Int32, Int32, T, IComparer<T>) Searches a range of elements in a one-dimensional sorted Array for a value, using the specified IComparer<T> generic interface.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library Clear Sets a range of elements in the Array to zero, to false, or to null, depending on the element type.
Public method Supported by the XNA Framework Clone Creates a shallow copy of the Array.
Public method Static member ConstrainedCopy Copies a range of elements from an Array starting at the specified source index and pastes them to another Array starting at the specified destination index. Guarantees that all changes are undone if the copy does not succeed completely.
Public method Static member ConvertAll<TInput, TOutput> Converts an array of one type to an array of another type.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library Copy(Array, Array, Int32) Copies a range of elements from an Array starting at the first element and pastes them into another Array starting at the first element. The length is specified as a 32-bit integer.
Public method Static member Copy(Array, Array, Int64) Copies a range of elements from an Array starting at the first element and pastes them into another Array starting at the first element. The length is specified as a 64-bit integer.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library Copy(Array, Int32, Array, Int32, Int32) Copies a range of elements from an Array starting at the specified source index and pastes them to another Array starting at the specified destination index. The length and the indexes are specified as 32-bit integers.
Public method Static member Copy(Array, Int64, Array, Int64, Int64) Copies a range of elements from an Array starting at the specified source index and pastes them to another Array starting at the specified destination index. The length and the indexes are specified as 64-bit integers.
Public method Supported by the XNA Framework CopyTo(Array, Int32) Copies all the elements of the current one-dimensional Array to the specified one-dimensional Array starting at the specified destination Array index. The index is specified as a 32-bit integer.
Public method CopyTo(Array, Int64) Copies all the elements of the current one-dimensional Array to the specified one-dimensional Array starting at the specified destination Array index. The index is specified as a 64-bit integer.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library CreateInstance(Type, Int32) Creates a one-dimensional Array of the specified Type and length, with zero-based indexing.
Public method Static member Supported by the XNA Framework CreateInstance(Type, Int32[]) Creates a multidimensional Array of the specified Type and dimension lengths, with zero-based indexing. The dimension lengths are specified in an array of 32-bit integers.
Public method Static member CreateInstance(Type, Int64[]) Creates a multidimensional Array of the specified Type and dimension lengths, with zero-based indexing. The dimension lengths are specified in an array of 64-bit integers.
Public method Static member CreateInstance(Type, Int32, Int32) Creates a two-dimensional Array of the specified Type and dimension lengths, with zero-based indexing.
Public method Static member CreateInstance(Type, Int32[], Int32[]) Creates a multidimensional Array of the specified Type and dimension lengths, with the specified lower bounds.
Public method Static member CreateInstance(Type, Int32, Int32, Int32) Creates a three-dimensional Array of the specified Type and dimension lengths, with zero-based indexing.
Public method Supported by the XNA Framework Supported by Portable Class Library Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Public method Static member Exists<T> Determines whether the specified array contains elements that match the conditions defined by the specified predicate.
Protected method Supported by the XNA Framework Supported by Portable Class Library Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method Static member Find<T> Searches for an element that matches the conditions defined by the specified predicate, and returns the first occurrence within the entire Array.
Public method Static member FindAll<T> Retrieves all the elements that match the conditions defined by the specified predicate.
Public method Static member FindIndex<T>(T[], Predicate<T>) Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the entire Array.
Public method Static member FindIndex<T>(T[], Int32, Predicate<T>) Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the range of elements in the Array that extends from the specified index to the last element.
Public method Static member FindIndex<T>(T[], Int32, Int32, Predicate<T>) Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the range of elements in the Array that starts at the specified index and contains the specified number of elements.
Public method Static member FindLast<T> Searches for an element that matches the conditions defined by the specified predicate, and returns the last occurrence within the entire Array.
Public method Static member FindLastIndex<T>(T[], Predicate<T>) Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the last occurrence within the entire Array.
Public method Static member FindLastIndex<T>(T[], Int32, Predicate<T>) Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the last occurrence within the range of elements in the Array that extends from the first element to the specified index.
Public method Static member FindLastIndex<T>(T[], Int32, Int32, Predicate<T>) Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the last occurrence within the range of elements in the Array that contains the specified number of elements and ends at the specified index.
Public method Static member ForEach<T> Performs the specified action on each element of the specified array.
Public method Supported by the XNA Framework Supported by Portable Class Library GetEnumerator Returns an IEnumerator for the Array.
Public method Supported by the XNA Framework Supported by Portable Class Library GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method Supported by the XNA Framework Supported by Portable Class Library GetLength Gets a 32-bit integer that represents the number of elements in the specified dimension of the Array.
Public method GetLongLength Gets a 64-bit integer that represents the number of elements in the specified dimension of the Array.
Public method Supported by the XNA Framework Supported by Portable Class Library GetLowerBound Gets the lower bound of the specified dimension in the Array.
Public method Supported by the XNA Framework Supported by Portable Class Library GetType Gets the Type of the current instance. (Inherited from Object.)
Public method Supported by the XNA Framework Supported by Portable Class Library GetUpperBound Gets the upper bound of the specified dimension in the Array.
Public method Supported by the XNA Framework Supported by Portable Class Library GetValue(Int32) Gets the value at the specified position in the one-dimensional Array. The index is specified as a 32-bit integer.
Public method Supported by the XNA Framework Supported by Portable Class Library GetValue(Int32[]) Gets the value at the specified position in the multidimensional Array. The indexes are specified as an array of 32-bit integers.
Public method GetValue(Int64) Gets the value at the specified position in the one-dimensional Array. The index is specified as a 64-bit integer.
Public method GetValue(Int64[]) Gets the value at the specified position in the multidimensional Array. The indexes are specified as an array of 64-bit integers.
Public method GetValue(Int32, Int32) Gets the value at the specified position in the two-dimensional Array. The indexes are specified as 32-bit integers.
Public method GetValue(Int64, Int64) Gets the value at the specified position in the two-dimensional Array. The indexes are specified as 64-bit integers.
Public method GetValue(Int32, Int32, Int32) Gets the value at the specified position in the three-dimensional Array. The indexes are specified as 32-bit integers.
Public method GetValue(Int64, Int64, Int64) Gets the value at the specified position in the three-dimensional Array. The indexes are specified as 64-bit integers.
Public method Static member IndexOf(Array, Object) Searches for the specified object and returns the index of the first occurrence within the entire one-dimensional Array.
Public method Static member IndexOf(Array, Object, Int32) Searches for the specified object and returns the index of the first occurrence within the range of elements in the one-dimensional Array that extends from the specified index to the last element.
Public method Static member Supported by the XNA Framework IndexOf(Array, Object, Int32, Int32) Searches for the specified object and returns the index of the first occurrence within the range of elements in the one-dimensional Array that starts at the specified index and contains the specified number of elements.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library IndexOf<T>(T[], T) Searches for the specified object and returns the index of the first occurrence within the entire Array.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library IndexOf<T>(T[], T, Int32) Searches for the specified object and returns the index of the first occurrence within the range of elements in the Array that extends from the specified index to the last element.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library IndexOf<T>(T[], T, Int32, Int32) Searches for the specified object and returns the index of the first occurrence within the range of elements in the Array that starts at the specified index and contains the specified number of elements.
Public method Supported by the XNA Framework Supported by Portable Class Library Initialize Initializes every element of the value-type Array by calling the default constructor of the value type.
Public method Static member LastIndexOf(Array, Object) Searches for the specified object and returns the index of the last occurrence within the entire one-dimensional Array.
Public method Static member LastIndexOf(Array, Object, Int32) Searches for the specified object and returns the index of the last occurrence within the range of elements in the one-dimensional Array that extends from the first element to the specified index.
Public method Static member Supported by the XNA Framework LastIndexOf(Array, Object, Int32, Int32) Searches for the specified object and returns the index of the last occurrence within the range of elements in the one-dimensional Array that contains the specified number of elements and ends at the specified index.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library LastIndexOf<T>(T[], T) Searches for the specified object and returns the index of the last occurrence within the entire Array.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library LastIndexOf<T>(T[], T, Int32) Searches for the specified object and returns the index of the last occurrence within the range of elements in the Array that extends from the first element to the specified index.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library LastIndexOf<T>(T[], T, Int32, Int32) Searches for the specified object and returns the index of the last occurrence within the range of elements in the Array that contains the specified number of elements and ends at the specified index.
Protected method Supported by the XNA Framework Supported by Portable Class Library MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method Static member Supported by the XNA Framework Supported by Portable Class Library Resize<T> Changes the number of elements of an array to the specified new size.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library Reverse(Array) Reverses the sequence of the elements in the entire one-dimensional Array.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library Reverse(Array, Int32, Int32) Reverses the sequence of the elements in a range of elements in the one-dimensional Array.
Public method Supported by the XNA Framework Supported by Portable Class Library SetValue(Object, Int32) Sets a value to the element at the specified position in the one-dimensional Array. The index is specified as a 32-bit integer.
Public method Supported by the XNA Framework Supported by Portable Class Library SetValue(Object, Int32[]) Sets a value to the element at the specified position in the multidimensional Array. The indexes are specified as an array of 32-bit integers.
Public method SetValue(Object, Int64) Sets a value to the element at the specified position in the one-dimensional Array. The index is specified as a 64-bit integer.
Public method SetValue(Object, Int64[]) Sets a value to the element at the specified position in the multidimensional Array. The indexes are specified as an array of 64-bit integers.
Public method SetValue(Object, Int32, Int32) Sets a value to the element at the specified position in the two-dimensional Array. The indexes are specified as 32-bit integers.
Public method SetValue(Object, Int64, Int64) Sets a value to the element at the specified position in the two-dimensional Array. The indexes are specified as 64-bit integers.
Public method SetValue(Object, Int32, Int32, Int32) Sets a value to the element at the specified position in the three-dimensional Array. The indexes are specified as 32-bit integers.
Public method SetValue(Object, Int64, Int64, Int64) Sets a value to the element at the specified position in the three-dimensional Array. The indexes are specified as 64-bit integers.
Public method Static member Supported by the XNA Framework Sort(Array) Sorts the elements in an entire one-dimensional Array using the IComparable implementation of each element of the Array.
Public method Static member Sort(Array, Array) Sorts a pair of one-dimensional Array objects (one contains the keys and the other contains the corresponding items) based on the keys in the first Array using the IComparable implementation of each key.
Public method Static member Supported by the XNA Framework Sort(Array, IComparer) Sorts the elements in a one-dimensional Array using the specified IComparer.
Public method Static member Supported by the XNA Framework Sort(Array, Array, IComparer) Sorts a pair of one-dimensional Array objects (one contains the keys and the other contains the corresponding items) based on the keys in the first Array using the specified IComparer.
Public method Static member Sort(Array, Int32, Int32) Sorts the elements in a range of elements in a one-dimensional Array using the IComparable implementation of each element of the Array.
Public method Static member Sort(Array, Array, Int32, Int32) Sorts a range of elements in a pair of one-dimensional Array objects (one contains the keys and the other contains the corresponding items) based on the keys in the first Array using the IComparable implementation of each key.
Public method Static member Supported by the XNA Framework Sort(Array, Int32, Int32, IComparer) Sorts the elements in a range of elements in a one-dimensional Array using the specified IComparer.
Public method Static member Supported by the XNA Framework Sort(Array, Array, Int32, Int32, IComparer) Sorts a range of elements in a pair of one-dimensional Array objects (one contains the keys and the other contains the corresponding items) based on the keys in the first Array using the specified IComparer.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library Sort<T>(T[]) Sorts the elements in an entire Array using the IComparable<T> generic interface implementation of each element of the Array.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library Sort<T>(T[], IComparer<T>) Sorts the elements in an Array using the specified IComparer<T> generic interface.
Public method Static member Supported by the XNA Framework Sort<T>(T[], Comparison<T>) Sorts the elements in an Array using the specified Comparison<T>.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library Sort<T>(T[], Int32, Int32) Sorts the elements in a range of elements in an Array using the IComparable<T> generic interface implementation of each element of the Array.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library Sort<T>(T[], Int32, Int32, IComparer<T>) Sorts the elements in a range of elements in an Array using the specified IComparer<T> generic interface.
Public method Static member Supported by the XNA Framework Sort<TKey, TValue>(TKey[], TValue[]) Sorts a pair of Array objects (one contains the keys and the other contains the corresponding items) based on the keys in the first Array using the IComparable<T> generic interface implementation of each key.
Public method Static member Supported by the XNA Framework Sort<TKey, TValue>(TKey[], TValue[], IComparer<TKey>) Sorts a pair of Array objects (one contains the keys and the other contains the corresponding items) based on the keys in the first Array using the specified IComparer<T> generic interface.
Public method Static member Supported by the XNA Framework Sort<TKey, TValue>(TKey[], TValue[], Int32, Int32) Sorts a range of elements in a pair of Array objects (one contains the keys and the other contains the corresponding items) based on the keys in the first Array using the IComparable<T> generic interface implementation of each key.
Public method Static member Supported by the XNA Framework Sort<TKey, TValue>(TKey[], TValue[], Int32, Int32, IComparer<TKey>) Sorts a range of elements in a pair of Array objects (one contains the keys and the other contains the corresponding items) based on the keys in the first Array using the specified IComparer<T> generic interface.
Public method Supported by the XNA Framework Supported by Portable Class Library ToString Returns a string that represents the current object. (Inherited from Object.)
Public method Static member Supported by the XNA Framework TrueForAll<T> Determines whether every element in the array matches the conditions defined by the specified predicate.
Top
  Name Description
Public Extension Method AsParallel Enables parallelization of a query. (Defined by ParallelEnumerable.)
Public Extension Method Supported by Portable Class Library AsQueryable Converts an IEnumerable to an IQueryable. (Defined by Queryable.)
Public Extension Method Supported by the XNA Framework Supported by Portable Class Library Cast<TResult> Casts the elements of an IEnumerable to the specified type. (Defined by Enumerable.)
Public Extension Method Supported by the XNA Framework Supported by Portable Class Library OfType<TResult> Filters the elements of an IEnumerable based on a specified type. (Defined by Enumerable.)
Top
  Name Description
Explicit interface implemetation Private method Supported by Portable Class Library ICollection.CopyTo Copies the elements of the Array to another Array, starting at the specified index.
Explicit interface implemetation Private property Supported by the XNA Framework Supported by Portable Class Library ICollection.Count Gets the number of elements contained in the Array.
Explicit interface implemetation Private property Supported by Portable Class Library ICollection.IsSynchronized Gets a value that indicates whether access to the Array is synchronized (thread safe).
Explicit interface implemetation Private property Supported by Portable Class Library ICollection.SyncRoot Gets an object that can be used to synchronize access to the Array.
Explicit interface implemetation Private method Supported by the XNA Framework Supported by Portable Class Library IList.Add Adds an item to IList.
Explicit interface implemetation Private method Supported by the XNA Framework Supported by Portable Class Library IList.Clear Removes all items from the IList.
Explicit interface implemetation Private method Supported by the XNA Framework Supported by Portable Class Library IList.Contains Determines whether an element is in the IList.
Explicit interface implemetation Private method Supported by the XNA Framework Supported by Portable Class Library IList.IndexOf Determines the index of a specific item in the IList.
Explicit interface implemetation Private method Supported by the XNA Framework Supported by Portable Class Library IList.Insert Inserts an item to the IList at the specified index.
Explicit interface implemetation Private property Supported by Portable Class Library IList.IsFixedSize Gets a value that indicates whether the Array has a fixed size.
Explicit interface implemetation Private property Supported by Portable Class Library IList.IsReadOnly Gets a value that indicates whether the Array is read-only.
Explicit interface implemetation Private property Supported by the XNA Framework Supported by Portable Class Library IList.Item Gets or sets the element at the specified index.
Explicit interface implemetation Private method Supported by the XNA Framework Supported by Portable Class Library IList.Remove Removes the first occurrence of a specific object from the IList.
Explicit interface implemetation Private method Supported by the XNA Framework Supported by Portable Class Library IList.RemoveAt Removes the IList item at the specified index.
Explicit interface implemetation Private method IStructuralComparable.CompareTo Determines whether the current collection object precedes, occurs in the same position as, or follows another object in the sort order.
Explicit interface implemetation Private method IStructuralEquatable.Equals Determines whether an object is equal to the current instance.
Explicit interface implemetation Private method IStructuralEquatable.GetHashCode Returns a hash code for the current instance.
Top

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. An array can have a maximum of 32 dimensions.

Important note Important

In the .NET Framework version 2.0, the Array class implements the System.Collections.Generic.IList<T>, System.Collections.Generic.ICollection<T>, and System.Collections.Generic.IEnumerable<T> 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 null.

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.

Using an Array object of pointers in native code is not supported and will throw a NotSupportedException for several methods.

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.
      System.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.
      System.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
*/


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
*/


.NET Framework

Supported in: 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

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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

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.

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Join in 4.0
@Jon_AP: See http://msdn.microsoft.com/en-us/library/57a79xd0.aspx#Y857 - the Join method is now part of the String class.
Where are the Trim and Join methods?
Why is there no Join(string) or Trim empty methods for arrays?  I would want to say something like:

";#My Value;#MyOtherValue;#".Split(";#").Trim().Join(", ")

Instead I have to write
string.Join(", ", ";#My Value;#MyOtherValue;#".Trim(";#".ToArrayChar()).Split(";#".ToArrayChar())

And even then it doesn't work because split breaks ";#" into two different characters to separately split on.