Share via


Array Class

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

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

Inheritance Hierarchy

System.Object
  System.Array

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

Syntax

'Declaration
<ComVisibleAttribute(True)> _
Public MustInherit Class Array _
    Implements IList, ICollection, IEnumerable, IStructuralComparable,  _
    IStructuralEquatable
[ComVisibleAttribute(true)]
public abstract class Array : IList, 
    ICollection, IEnumerable, IStructuralComparable, IStructuralEquatable

The Array type exposes the following members.

Properties

  Name Description
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 IsFixedSize Gets a value indicating whether the Array has a fixed size.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 IsReadOnly Gets a value indicating whether the Array is read-only.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 IsSynchronized Gets a value indicating whether access to the Array is synchronized (thread safe).
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 Length Gets a 32-bit integer that represents the total number of elements in all the dimensions of the Array; zero if there are no elements in the array.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 Rank Gets the zero-based rank (number of dimensions) of the Array.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 SyncRoot Gets an object that can be used to synchronize access to the Array.

Top

Methods

  Name Description
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 AsReadOnly<T> Returns a read-only wrapper for the specified array.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 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 methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 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 methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 BinarySearch<T>(array<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 methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 BinarySearch<T>(array<T[], T, IComparer<T>) Searches an entire one-dimensional sorted Array for a value using the specified IComparer<T> generic interface.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 BinarySearch<T>(array<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 methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 BinarySearch<T>(array<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 methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Clear Sets a range of elements in the Array to zero, to false, or to nulla null reference (Nothing in Visual Basic), depending on the element type.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Clone Creates a shallow copy of the Array.
Public methodStatic 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 methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 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 methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 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 methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 CopyTo 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 methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 CreateInstance(Type, Int32) Creates a one-dimensional Array of the specified Type and length, with zero-based indexing.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 CreateInstance(Type, array<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 methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 ForEach<T> Performs the specified action on each element of the specified array.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetEnumerator Returns an IEnumerator for the Array.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetLength Gets a 32-bit integer that represents the number of elements in the specified dimension of the Array.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetLowerBound Gets the lower bound of the specified dimension in the Array.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetType Gets the Type of the current instance. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetUpperBound Gets the upper bound of the specified dimension in the Array.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetValue(Int32) Gets the value at the specified position in the one-dimensional Array. The index is specified as a 32-bit integer.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetValue(array<Int32[]) Gets the value at the specified position in the multidimensional Array. The indexes are specified as an array of 32-bit integers.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 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 methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 IndexOf<T>(array<T[], T) Searches for the specified object and returns the index of the first occurrence within the entire Array.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 IndexOf<T>(array<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 methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 IndexOf<T>(array<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 methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Initialize Initializes every element of the value-type Array by calling the default constructor of the value type.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 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 methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 LastIndexOf<T>(array<T[], T) Searches for the specified object and returns the index of the last occurrence within the entire Array.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 LastIndexOf<T>(array<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 methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 LastIndexOf<T>(array<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 methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Resize<T> Changes the number of elements of an array to the specified new size.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Reverse(Array) Reverses the sequence of the elements in the entire one-dimensional Array.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Reverse(Array, Int32, Int32) Reverses the sequence of the elements in a range of elements in the one-dimensional Array.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 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 methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 SetValue(Object, array<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 methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Sort(Array) Sorts the elements in an entire one-dimensional Array using the IComparable implementation of each element of the Array.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Sort(Array, IComparer) Sorts the elements in a one-dimensional Array using the specified IComparer.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 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 methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Sort(Array, Int32, Int32, IComparer) Sorts the elements in a range of elements in a one-dimensional Array using the specified IComparer.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 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 methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Sort<T>(array<T[]) Sorts the elements in an entire Array using the IComparable<T> generic interface implementation of each element of the Array.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Sort<T>(array<T[], IComparer<T>) Sorts the elements in an Array using the specified IComparer<T> generic interface.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Sort<T>(array<T[], Comparison<T>) Sorts the elements in an Array using the specified Comparison<T>.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Sort<T>(array<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 methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Sort<T>(array<T[], Int32, Int32, IComparer<T>) Sorts the elements in a range of elements in an Array using the specified IComparer<T> generic interface.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Sort<TKey, TValue>(array<TKey[], array<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 methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Sort<TKey, TValue>(array<TKey[], array<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 methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Sort<TKey, TValue>(array<TKey[], array<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 methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Sort<TKey, TValue>(array<TKey[], array<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 methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Extension Methods

  Name Description
Public Extension MethodSupported by Silverlight for Windows Phone AsQueryable Converts an IEnumerable to an IQueryable. (Defined by Queryable.)
Public Extension MethodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Cast<TResult> Converts the elements of an IEnumerable to the specified type. (Defined by Enumerable.)
Public Extension MethodSupported by Silverlight for Windows PhoneSupported by Xbox 360 OfType<TResult> Filters the elements of an IEnumerable based on a specified type. (Defined by Enumerable.)

Top

Explicit Interface Implementations

  Name Description
Explicit interface implemetationPrivate propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 ICollection.Count Gets the number of elements contained in the Array.
Explicit interface implemetationPrivate methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 IList.Add Implements IList.Add. Throws a NotSupportedException in all cases.
Explicit interface implemetationPrivate methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 IList.Clear Sets all elements in the Array to zero, to false, or to nulla null reference (Nothing in Visual Basic), depending on the element type.
Explicit interface implemetationPrivate methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 IList.Contains Determines whether an element is in the Array.
Explicit interface implemetationPrivate methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 IList.IndexOf Searches for the specified object and returns the index of the first occurrence within the current one-dimensional instance.
Explicit interface implemetationPrivate methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 IList.Insert Implements IList.Insert. Throws a NotSupportedException in all cases.
Explicit interface implemetationPrivate propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 IList.Item Gets or sets the element at the specified index.
Explicit interface implemetationPrivate methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 IList.Remove Implements IList.Remove. Throws a NotSupportedException in all cases.
Explicit interface implemetationPrivate methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 IList.RemoveAt Implements IList.RemoveAt. Throws a NotSupportedException in all cases.
Explicit interface implemetationPrivate 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 implemetationPrivate method IStructuralEquatable.Equals Determines whether an object is equal to the current instance.
Explicit interface implemetationPrivate method IStructuralEquatable.GetHashCode Returns a hash code for the current instance.

Top

Remarks

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.

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 nulla 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.

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.

Examples

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

Imports System
Public Class Example

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

        ' Creates and initializes a new integer array and a new Object array.
        Dim myIntArray() As Integer = {1, 2, 3, 4, 5}
        Dim myObjArray() As Object = {26, 27, 28, 29, 30}

        ' Prints the initial values of both arrays.
        outputBlock.Text &= String.Format("Initially,") & vbCrLf
        outputBlock.Text &= "integer array:"
        PrintValues(outputBlock, myIntArray)
        outputBlock.Text &= "Object array: "
        PrintValues(outputBlock, 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.
        outputBlock.Text &= ControlChars.NewLine + "After copying the first two" _
           + " elements of the integer array to the Object array," & vbCrLf
        outputBlock.Text &= "integer array:"
        PrintValues(outputBlock, myIntArray)
        outputBlock.Text &= "Object array: "
        PrintValues(outputBlock, 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.
        outputBlock.Text &= ControlChars.NewLine + "After copying the last two" _
           + " elements of the Object array to the integer array," & vbCrLf
        outputBlock.Text &= "integer array:"
        PrintValues(outputBlock, myIntArray)
        outputBlock.Text &= "Object array: "
        PrintValues(outputBlock, myObjArray)
    End Sub

    Public Overloads Shared Sub PrintValues(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal myArr() As Object)
        Dim i As Object
        For Each i In myArr
            outputBlock.Text &= String.Format(ControlChars.Tab + "{0}", i)
        Next i
        outputBlock.Text &= vbCrLf
    End Sub

    Public Overloads Shared Sub PrintValues(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal myArr() As Integer)
        Dim i As Integer
        For Each i In myArr
            outputBlock.Text &= String.Format(ControlChars.Tab + "{0}", i)
        Next i
        outputBlock.Text &= vbCrLf
    End Sub
End Class

' 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
using System;
public class Example
{
    public static void Demo(System.Windows.Controls.TextBlock outputBlock)
    {

        // 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.
        outputBlock.Text += String.Format("Initially:") + "\n";
        outputBlock.Text += "integer array:";
        PrintValues(outputBlock, myIntArray);
        outputBlock.Text += "Object array: ";
        PrintValues(outputBlock, 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.
        outputBlock.Text += String.Format("\nAfter copying the first two elements of the integer array to the Object array,") + "\n";
        outputBlock.Text += "integer array:";
        PrintValues(outputBlock, myIntArray);
        outputBlock.Text += "Object array: ";
        PrintValues(outputBlock, 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.
        outputBlock.Text += String.Format("\nAfter copying the last two elements of the Object array to the integer array,") + "\n";
        outputBlock.Text += "integer array:";
        PrintValues(outputBlock, myIntArray);
        outputBlock.Text += "Object array: ";
        PrintValues(outputBlock, myObjArray);
    }


    public static void PrintValues(System.Windows.Controls.TextBlock outputBlock, Object[] myArr)
    {
        foreach (Object i in myArr)
        {
            outputBlock.Text += String.Format("   {0}", i);
        }
        outputBlock.Text += "\n";
    }

    public static void PrintValues(System.Windows.Controls.TextBlock outputBlock, int[] myArr)
    {
        foreach (int i in myArr)
        {
            outputBlock.Text += String.Format("   {0}", i);
        }
        outputBlock.Text += "\n";
    }
    /* 
    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 Example2

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

        ' Creates and initializes a new three-dimensional Array of
        ' type Int32.
        Dim myArr As Array = Array.CreateInstance(GetType(Int32), 2, 3, 4)
        Dim i As Integer
        For i = myArr.GetLowerBound(0) To myArr.GetUpperBound(0)
            Dim j As Integer
            For j = myArr.GetLowerBound(1) To myArr.GetUpperBound(1)
                Dim k As Integer
                For k = myArr.GetLowerBound(2) To myArr.GetUpperBound(2)
                    myArr.SetValue(i * 100 + j * 10 + k, i, j, k)
                Next k
            Next j
        Next i ' Displays the properties of the Array.
        outputBlock.Text &= String.Format("The Array has {0} dimension(s) & vbCrLf and a " _
           + "total of {1} elements.", myArr.Rank, myArr.Length)
        outputBlock.Text &= ControlChars.Tab + "Length" + ControlChars.Tab _
           + "Lower" + ControlChars.Tab + "Upper" & vbCrLf
        For i = 0 To myArr.Rank - 1
            outputBlock.Text &= String.Format("{0}:" + ControlChars.Tab + "{1}", i, _
               myArr.GetLength(i))
            outputBlock.Text &= String.Format(ControlChars.Tab + "{0}" + ControlChars.Tab _
               + "{1}", myArr.GetLowerBound(i), myArr.GetUpperBound(i)) & vbCrLf
        Next i

        ' Displays the contents of the Array.
        outputBlock.Text &= "The Array contains the following values:" & vbCrLf
        PrintValues(outputBlock, myArr)
    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 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 Example2
{

    public static void Demo(System.Windows.Controls.TextBlock outputBlock)
    {
        // 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.
        outputBlock.Text += String.Format("The Array has {0} dimension(s) and a total of {1} elements.", myArr.Rank, myArr.Length) + "\n";
        outputBlock.Text += "  Length  Lower  Upper" + "\n";
        for (int i = 0; i < myArr.Rank; i++)
        {
            outputBlock.Text += String.Format("{0}:  {1}", i, myArr.GetLength(i));
            outputBlock.Text += String.Format("  {0}  {1}", myArr.GetLowerBound(i), myArr.GetUpperBound(i)) + "\n";
        }

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


    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("   {0}", myEnumerator.Current);
        }
        outputBlock.Text += "\n";
    }
}

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.

Thread Safety

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.

See Also

Reference