System.Array Class

public abstract class Array : ICloneable, ICollection, IEnumerable, IList

Base Types

Object
  Array

This type implements ICloneable, ICollection, IEnumerable, and IList.

Assembly

mscorlib

Library

BCL

Summary

Serves as the base class for arrays. Provides methods for creating, copying, manipulating, searching, and sorting arrays.

Description

This class is intended to be used as a base class by language implementations that support arrays. Only the system can derive from this type: derived classes of Array are not to be created by the developer.

[Note: An array is a collection of identically typed data elements that are accessed and referenced by sets of integral indices.

The rank of an array is the number of dimensions in the array. Each dimension has its own set of indices. An array with a rank greater than one can have a different lower bound and a different number of elements for each dimension. Multidimensional arrays (i.e. arrays with a rank greater than one) are processed in row-major order.

The lower bound of a dimension is the starting index of that dimension.

The length of an array is the total number of elements contained in all of its dimensions.

A vector is a one-dimensional array with a lower bound of '0'.

If the implementer creates a derived class of Array, expected Array behavior cannot be guaranteed. For information on array-like objects with increased functionality, see the IList and IList<T> interfaces. For more information regarding the use of arrays versus the use of collections, see Partition V of the CLI Specification.

]

Every specific Array type has three instance methods defined on it. While some programming languages allow direct access to these methods, they are primarily intended to be called by the output of compilers based on language syntax that deals with arrays.

In addition, every specific Array type has a constructor on it that takes as many non-negative Int32 arguments as the array has dimensions. The arguments specify the number of elements in each dimension, and a lower bound of 0. Thus, a two-dimensional array of Int32 objects would have a constructor that could be called with (2, 4) as its arguments to create an array of eight zeros with the first dimension indexed with 0 and 1 and the second dimension indexed with 0, 1, 2, and 3.

For all specific array types except vectors (i.e. those permitted to have non-zero lower bounds and those with more than one dimension) there is an additional constructor. It takes twice as many arguments as the array has dimensions. The arguments are considered in pairs, with the first of the pair specifying the lower bound for that dimension and the second specifying the total number of elements in that dimension. Thus, a two-dimensional array of Int32 objects would also have a constructor that could be called with (-1, 2, 1, 3) as its arguments, specifying an array of 6 zeros, with the first dimension indexed by -1 and 0, and the second dimension indexed by 1, 2, and 3.

Enumeration over an array occurs in ascending row-major order, starting from the first element. (For example, a 2x3 array is traversed in the order [0,0], [0,1], [0,2], [1,0], [1,1], and [1,2].)

Parallel implementation of methods taking a System.Predicate argument are not permitted.

See Also

System Namespace

Members

Array Constructors

Array Constructor

Array Methods

Array.AsReadOnly<T> Method
Array.BinarySearch(System.Array, int, int, System.Object, System.Collections.IComparer) Method
Array.BinarySearch(System.Array, System.Object, System.Collections.IComparer) Method
Array.BinarySearch(System.Array, int, int, System.Object) Method
Array.BinarySearch(System.Array, System.Object) Method
Array.BinarySearch<T>(T[], T) Method
Array.BinarySearch<T>(T[], T, System.Collections.Generic.IComparer<T>) Method
Array.BinarySearch<T>(T[], int, int, T) Method
Array.BinarySearch<T>(T[], int, int, T, System.Collections.Generic.IComparer<T>) Method
Array.Clear Method
Array.Clone Method
Array.ConvertAll<T,U> Method
Array.Copy(System.Array, System.Array, int) Method
Array.Copy(System.Array, int, System.Array, int, int) Method
Array.CopyTo Method
Array.CreateInstance(System.Type, int[]) Method
Array.CreateInstance(System.Type, int, int, int) Method
Array.CreateInstance(System.Type, int, int) Method
Array.CreateInstance(System.Type, int) Method
Array.CreateInstance(System.Type, int[], int[]) Method
Array.Exists<T> Method
Array.Find<T> Method
Array.FindAll<T> Method
Array.FindIndex<T>(T[], System.Predicate<T>) Method
Array.FindIndex<T>(T[], int, System.Predicate<T>) Method
Array.FindIndex<T>(T[], int, int, System.Predicate<T>) Method
Array.FindLast<T> Method
Array.FindLastIndex<T>(T[], System.Predicate<T>) Method
Array.FindLastIndex<T>(T[], int, System.Predicate<T>) Method
Array.FindLastIndex<T>(T[], int, int, System.Predicate<T>) Method
Array.ForEach<T> Method
Array.GetEnumerator Method
Array.GetLength Method
Array.GetLowerBound Method
Array.GetUpperBound Method
Array.GetValue(int[]) Method
Array.GetValue(int) Method
Array.GetValue(int, int) Method
Array.GetValue(int, int, int) Method
Array.IndexOf(System.Array, System.Object, int, int) Method
Array.IndexOf(System.Array, System.Object, int) Method
Array.IndexOf(System.Array, System.Object) Method
Array.IndexOf<T>(T[], T, int, int) Method
Array.IndexOf<T>(T[], T, int) Method
Array.IndexOf<T>(T[], T) Method
Array.Initialize Method
Array.LastIndexOf(System.Array, System.Object, int, int) Method
Array.LastIndexOf(System.Array, System.Object, int) Method
Array.LastIndexOf(System.Array, System.Object) Method
Array.LastIndexOf<T>(T[], T, int, int) Method
Array.LastIndexOf<T>(T[], T, int) Method
Array.LastIndexOf<T>(T[], T) Method
Array.Resize<T> Method
Array.Reverse(System.Array, int, int) Method
Array.Reverse(System.Array) Method
Array.SetValue(System.Object, int) Method
Array.SetValue(System.Object, int, int) Method
Array.SetValue(System.Object, int, int, int) Method
Array.SetValue(System.Object, int[]) Method
Array.Sort(System.Array, System.Array, int, int, System.Collections.IComparer) Method
Array.Sort(System.Array, int, int, System.Collections.IComparer) Method
Array.Sort(System.Array, System.Array, System.Collections.IComparer) Method
Array.Sort(System.Array, System.Collections.IComparer) Method
Array.Sort(System.Array, System.Array, int, int) Method
Array.Sort(System.Array) Method
Array.Sort(System.Array, System.Array) Method
Array.Sort(System.Array, int, int) Method
Array.Sort<K,V>(K[], V[], int, int, System.Collections.Generic.IComparer<K>) Method
Array.Sort<K,V>(K[], V[], System.Collections.Generic.IComparer<K>) Method
Array.Sort<K,V>(K[], V[], int, int) Method
Array.Sort<K,V>(K[], V[]) Method
Array.Sort<T>(T[], int, int, System.Collections.Generic.IComparer<T>) Method
Array.Sort<T>(T[], System.Collections.Generic.IComparer<T>) Method
Array.Sort<T>(T[], System.Comparison<T>) Method
Array.Sort<T>(T[]) Method
Array.Sort<T>(T[], int, int) Method
Array.System.Collections.IList.Add Method
Array.System.Collections.IList.Clear Method
Array.System.Collections.IList.Contains Method
Array.System.Collections.IList.IndexOf Method
Array.System.Collections.IList.Insert Method
Array.System.Collections.IList.Remove Method
Array.System.Collections.IList.RemoveAt Method
Array.TrueForAll<T> Method

Array Properties

Array.IsFixedSize Property
Array.IsReadOnly Property
Array.IsSynchronized Property
Array.Length Property
Array.LongLength Property
Array.Rank Property
Array.SyncRoot Property
Array.System.Collections.ICollection.Count Property
Array.System.Collections.IList.Item Property


Array Constructor

private Array();

Summary

Constructs a new instance of the Array class.

See Also

System.Array Class, System Namespace

Array.AsReadOnly<T> Method

public static IList<T> AsReadOnly<T>(T[] array)

Summary

Returns a read-only System.Collections.Generic.IList<T> wrapper around the specified array.

Parameters

array
The array to wrap in a read-only IList<T> wrapper.

Return Value

A read-only IList<T> wrapper around the specified array.

Exceptions

Exception TypeCondition
ArgumentNullExceptionarray is null .

Description

[Note: To prevent any modifications to the array, expose the array only through this wrapper.]

The returned IList<T> has the same enumeration order as the array it wraps.

A collection that is read-only is simply a collection with a wrapper that prevents modifying the underlying array; therefore, if changes are made to the underlying array, the read-only collection reflects those changes.

See Also

System.Array Class, System Namespace

Array.BinarySearch(System.Array, int, int, System.Object, System.Collections.IComparer) Method

public static int BinarySearch(Array array, int index, int length, object value, IComparer comparer);

Summary

Searches the specified section of the specified one-dimensional Array for the specified value, using the specified IComparer implementation.

Parameters

array
A Array to search.
index
A Int32 that contains the index at which searching starts.
length
A Int32 that contains the number of elements to search, beginning with index .
value
A Object for which to search.
comparer
The IComparer implementation to use when comparing elements. Specify a null reference to use the IComparable implementation of each element.

Return Value

A Int32 with one of the following values based on the result of the search operation.

Return ValueDescription
The index of value in the array.value was found.
The bitwise complement of the index of the first element that is larger than value.value was not found, and at least one array element in the range of index to index + length - 1 was greater than value.
The bitwise complement of (index + length).value was not found, and value was greater than all array elements in the range of index to index + length- 1.
[Note: If value is not found, the caller can take the bitwise complement of the return value to determine the index of array where value would be found in the range of index to index + length - 1 if array is already sorted.]

Exceptions

Exception TypeCondition
ArgumentNullExceptionarray is null .
RankExceptionarray has more than one dimension.
ArgumentOutOfRangeExceptionindex is less than array.GetLowerBound(0) .

-or-

length is less than zero.

ArgumentExceptionindex + length is greater than array.GetLowerBound(0) + array.Length .

-or-

array.UpperBound == System.Int32.MaxValue.

InvalidOperationExceptioncomparer is null , and both value and at least one element of array do not implement the IComparable interface.

Description

value is compared to each element of array using comparer until an element with a value greater than or equal to value is found. If comparer is null , the IComparable interface of the element being compared - or of value if the element being compared does not implement the interface -- is used. If value does not implement the IComparable interface and is compared to an element that does not implement the IComparable interface, a InvalidOperationException exception is thrown. If array is not already sorted, correct results are not guaranteed.

[Note: A null reference can be compared with any type; therefore, comparisons with a null reference do not generate exceptions.]

Example

This example demonstrates the System.Array.BinarySearch(System.Array,System.Object) method.

using System;
class BinarySearchExample {
  public static void Main() {    
    int[] intAry = { 0, 2, 4, 6, 8 };
    Console.WriteLine( "The indices and elements of the array are: ");
    for ( int i = 0; i < intAry.Length; i++ )
      Console.Write("[{0}]: {1, -5}", i, intAry[i]);
    Console.WriteLine();
    SearchFor( intAry, 3 );
    SearchFor( intAry, 6 );
    SearchFor( intAry, 9 );
  }
  public static void SearchFor( Array ar, Object value ) {
    int i = Array.BinarySearch( ar, 0, ar.Length, value, null );
    Console.WriteLine();
    if ( i > 0 ) {
      Console.Write( "The object searched for, {0}, was found ", value );
      Console.WriteLine( "at index {1}.", value, i );
    }
    else if ( ~i == ar.Length ) {
      Console.Write( "The object searched for, {0}, was ", value );
      Console.Write( "not found,\nand no object in the array had " );
      Console.WriteLine( "greater value. " );
    }
    else {
      Console.Write( "The object searched for, {0}, was ", value );
      Console.Write( "not found.\nThe next larger object is at " );
      Console.WriteLine( "index {0}.", ~i );
    }
  }
}
   
The output is

The indices and elements of the array are:

[0]:0 [1]:2 [2]:4 [3]:6 [4]:8

The object searched for, 3, was not found.

The next larger object is at index 2.

The object searched for, 6, was found at index 3.

The object searched for, 9, was not found,

and no object in the array had greater value.

See Also

System.Array Class, System Namespace

Array.BinarySearch(System.Array, System.Object, System.Collections.IComparer) Method

public static int BinarySearch(Array array, object value, IComparer comparer);

Summary

Searches the specified one-dimensional Array for the specified value, using the specified IComparer implementation.

Parameters

array
A Array to search.
value
A Object for which to search.
comparer
The IComparer implementation to use when comparing elements. Specify a null reference to use the IComparable implementation of each element.

Return Value

A Int32 with one of the following values based on the result of the search operation.

Return ValueDescription
The index of value in the array.value was found.
The bitwise complement of the index of the first element that is larger than value.value was not found, and at least one array element was greater than value.
The bitwise complement of (array.GetLowerBound(0) + array.Length).value was not found, and value was greater than all array elements.
[Note: If value is not found, the caller can take the bitwise complement of the return value to determine the index where value would be found in array if it is already sorted.]

Exceptions

Exception TypeCondition
ArgumentNullExceptionarray is null .
RankExceptionarray has more than one dimension.
InvalidOperationExceptioncomparer is null , and both value and at least one element of array do not implement the IComparable interface.

Description

This version of System.Array.BinarySearch(System.Array,System.Object) is equivalent to System.Array.BinarySearch(System.Array,System.Object)(array, array.GetLowerBound(0), array.Length, value, comparer).

value is compared to each element of array using comparer until an element with a value greater than or equal to value is found. If comparer is null , the IComparable interface of the element being compared - or of value if the element being compared does not implement the interface - is used. If value does not implement the IComparable interface and is compared to an element that does not implement the IComparable interface, a InvalidOperationException exception is thrown. If array is not already sorted, correct results are not guaranteed.

[Note: A null reference can be compared with any type; therefore, comparisons with a null reference do not generate exceptions.]

See Also

System.Array Class, System Namespace

Array.BinarySearch(System.Array, int, int, System.Object) Method

public static int BinarySearch(Array array, int index, int length, object value);

Summary

Searches the specified section of the specified one-dimensional Array for the specified value.

Parameters

array
A Array to search.
index
A Int32 that contains the index at which searching starts.
length
A Int32 that contains the number of elements to search, beginning with index .
value
A Object for which to search.

Return Value

A Int32 with one of the following values based on the result of the search operation.

Return ValueDescription
The index of value in the array.value was found.
The bitwise complement of the index of the first element that is larger than value.value was not found, and at least one array element in the range of index to index + length - 1 was greater than value.
The bitwise complement of (index + length).value was not found, and value was greater than all array elements in the range of index to index + length- 1.
[Note: If value is not found, the caller can take the bitwise complement of the return value to determine the index of the array where value would be found in the range of index to index + length - 1 if array is already sorted.]

Exceptions

Exception TypeCondition
ArgumentNullExceptionarray is null .
RankExceptionarray has more than one dimension.
ArgumentOutOfRangeExceptionindex < array.GetLowerBound(0).

-or-

length < 0.

ArgumentExceptionindex and length do not specify a valid range in array (i.e. index + length > array.GetLowerBound(0) + array.Length).

-or-

array.UpperBound == System.Int32.MaxValue.

InvalidOperationExceptionEither value or at least one element of array does not implement the IComparable interface.

Description

This version of System.Array.BinarySearch(System.Array,System.Object) is equivalent to System.Array.BinarySearch(System.Array,System.Object)(array, array.GetLowerBound(0), array.Length, value, null ).

value is compared to each element of array using the IComparable interface of the element being compared - or of value if the element being compared does not implement the interface - until an element with a value greater than or equal to value is found. If value does not implement the IComparable interface and is compared to an element that does not implement the IComparable interface, a InvalidOperationException exception is thrown. If array is not already sorted, correct results are not guaranteed.

[Note: A null reference can be compared with any type; therefore, comparisons with a null reference do not generate exceptions.]

See Also

System.Array Class, System Namespace

Array.BinarySearch(System.Array, System.Object) Method

public static int BinarySearch(Array array, object value);

Summary

Searches the specified one-dimensional Array for the specified object.

Parameters

array
A Array to search for an object.
value
A Object for which to search.

Return Value

A Int32 with one of the following values based on the result of the search operation.

Return Value Description
The index of value in the array.value was found.
The bitwise complement of the index of the first element that is larger than value.value was not found and the value of at least one element of array was greater than value.
The bitwise complement of (array.GetLowerBound(0) + array.Length).value was not found, and value was greater than the value of all array elements.
[Note: If value is not found, the caller can take the bitwise complement of the return value to determine the index where value would be found in array if it is sorted already.]

Exceptions

Exception TypeCondition
ArgumentNullExceptionarray is null .
RankExceptionarray has more than one dimension.

InvalidOperationExceptionBoth value and at least one element of array do not implement the IComparable interface.

Description

This version of System.Array.BinarySearch(System.Array,System.Object) is equivalent to System.Array.BinarySearch(System.Array,System.Object)(array, array.GetLowerBound(0), array.Length, value, null ).

value is compared to each element of array using the IComparable interface of the element being compared - or of value if the element being compared does not implement the interface - until an element with a value greater than or equal to value is found. If value does not implement the IComparable interface and is compared to an element that does not implement the IComparable interface, a InvalidOperationException exception is thrown. If array is not already sorted, correct results are not guaranteed.

[Note: A null reference can be compared with any type; therefore, comparisons with a null reference do not generate exceptions.]

See Also

System.Array Class, System Namespace

Array.BinarySearch<T>(T[], T) Method

public static int BinarySearch<T>(T[] array, T value)

Summary

Searches an entire one-dimensional sorted array for a specific element, using the IComparable<T> or IComparable interface implemented by each element of the array and by the specified object.

Parameters

array
The one-dimensional array to search.
value
The object for which to search.

Return Value

One of the following values based on the result of the search operation:

Return ValueDescription
A non-negative index of value in the array.value was found.
A negative value, which is the bitwise complement of the index of the first element that is larger than value.value was not found and the value of at least one element of array was greater than value.
A negative value, which is the bitwise complement of one more than the index of the final element.value was not found, and value was greater than the value of all array elements.

Exceptions

Exception TypeCondition
ArgumentNullExceptionarray is null .
InvalidOperationExceptionNeither value nor the elements of the array implement the IComparable<T> or IComparable interfaces.

Description

Either value or every element of array must implement the IComparable<T> or IComparable interface, which is used for comparisons. The elements of array must already be sorted in increasing value according to the sort order defined by the IComparable<T> or IComparable implementation; otherwise, the behavior is unspecified

Duplicate elements are allowed. If the array contains more than one element equal to value, the method returns the index of only one of the occurrences, but not necessarily the first one.

[Note: null can always be compared with any other reference type; therefore, comparisons with null do not generate an exception.]

See Also

System.Array Class, System Namespace

Array.BinarySearch<T>(T[], T, System.Collections.Generic.IComparer<T>) Method

public static int BinarySearch<T>(T[] array, T value, IComparer<T> comparer)

Summary

Searches an entire one-dimensional sorted array for a value using the specified IComparer<T> interface.

Parameters

array
The one-dimensional array to search.
value
The object for which to search.
comparer
The implementation to use when comparing elements.

-or-

null to use the IComparable<T> or IComparable implementation of each element.

Return Value

One of the following values based on the result of the search operation:

Return ValueDescription
A non-negative index of value in the array.value was found.
A negative value, which is the bitwise complement of the index of the first element that is larger than value.value was not found and the value of at least one element of array was greater than value.
A negative value, which is the bitwise complement of one more than the index of the final element.value was not found, and value was greater than the value of all array elements.

Exceptions

Exception TypeCondition
ArgumentNullExceptionarray is null .
InvalidOperationExceptioncomparer is null , and neither value nor the elements of the array implement the IComparable<T> or IComparable interface.

Description

The comparer customizes how the elements are compared.

The elements of array must already be sorted in increasing value according to the sort order defined by comparer; otherwise, the behavior is unspecified

If comparer is not null , the elements of array are compared to the specified value using the specified System.Collections.Generic.IComparer implementation.

If comparer is null , the default comparer is used.

Duplicate elements are allowed. If the array contains more than one element equal to value, the method returns the index of only one of the occurrences, but not necessarily the first one.

[Note: null can always be compared with any other reference type; therefore, comparisons with null do not generate an exception.]

See Also

System.Array Class, System Namespace

Array.BinarySearch<T>(T[], int, int, T) Method

public static int BinarySearch<T>(T[] array, int index, int length, T value)

Summary

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.

Parameters

array
The one-dimensional array to search.
index
The starting index of the range to search.
length
The length of the range to search.
value
The object for which to search.

Return Value

One of the following values based on the result of the search operation:

Return ValueDescription
A non-negative index of value in the array.value was found.
A negative value, which is the bitwise complement of the index of the first element that is larger than value.value was not found and the value of at least one element of array was greater than value.
A negative value, which is the bitwise complement of one more than the index of the final element.value was not found, and value was greater than the value of all array elements.

Exceptions

Exception TypeCondition
ArgumentExceptionindex + length is greater than array.Length .

ArgumentNullExceptionarray is null .
ArgumentOutOfRangeExceptionindex is less than zero

-or-

length is less than zero.

InvalidOperationExceptionNeither value nor the elements of the array implement the IComparable<T> or IComparable interface.

Description

Either value or every element of array must implement the IComparable interface, which is used for comparisons. The elements of array must already be sorted in increasing value according to the sort order defined by the IComparable<T> or IComparable implementation; otherwise, the behavior is unspecified

Duplicate elements are allowed. If the array contains more than one element equal to value, the method returns the index of only one of the occurrences, but not necessarily the first one.

[Note: null can always be compared with any other reference type; therefore, comparisons with null do not generate an exception.]

See Also

System.Array Class, System Namespace

Array.BinarySearch<T>(T[], int, int, T, System.Collections.Generic.IComparer<T>) Method

public static int BinarySearch<T>(T[] array, int index, int length, T value, IComparer<T> comparer)

Summary

Searches a range of elements in a one-dimensional sorted array for a value, using the specified IComparer<T> interface.

Parameters

array
The one-dimensional array to search.
index
The starting index of the range to search.
length
The length of the range to search.
value
The object for which to search.
comparer
The implementation to use when comparing elements.

-or-

null to use the IComparable<T> or IComparable implementation of each element.

Return Value

One of the following values based on the result of the search operation:

Return ValueDescription
A non-negative index of value in the array.value was found.
A negative value, which is the bitwise complement of the index of the first element that is larger than value.value was not found and the value of at least one element of array was greater than value.
A negative value, which is the bitwise complement of one more than the index of the final element.value was not found, and value was greater than the value of all array elements.

Exceptions

Exception TypeCondition
ArgumentExceptionindex and length do not specify a valid range in array.

ArgumentNullExceptionarray is null .
ArgumentOutOfRangeExceptionindex is less than zero

-or-

length is less than zero.

InvalidOperationExceptioncomparer is null , and neither value nor the elements of the array implement the IComparable<T> or IComparable interface.

Description

The comparer customizes how the elements are compared.

The elements of array must already be sorted in increasing value according to the sort order defined by comparer; otherwise, the behavior is unspecified.

If comparer is not null , the elements of array are compared to the specified value using the specified IComparer<T> implementation.

If comparer is null , the comparison is done using the IComparable<T> or System.IComparable implementation provided by the element itself or by the specified value.

Duplicate elements are allowed. If the array contains more than one element equal to value, the method returns the index of only one of the occurrences, but not necessarily the first one.

[Note: null can always be compared with any other reference type; therefore, comparisons with null do not generate an exception.]

See Also

System.Array Class, System Namespace

Array.Clear Method

public static void Clear(Array array, int index, int length);

Summary

Sets the specified range of elements in the specified Array to zero, false, or to a null reference, depending on the element type.

Parameters

array
The Array to clear.
index
A Int32 that contains the index at which clearing starts.
length
A Int32 that contains the number of elements to clear, beginning with index.

Exceptions

Exception TypeCondition
ArgumentNullExceptionarray is null .
ArgumentOutOfRangeExceptionindex < array.GetLowerBound(0).

length < 0.

index and length do not specify a valid range in array (i.e. index + length > array.GetLowerBound(0) + array.Length ).

Description

Reference-type elements will be set to null . Value-type elements will be set to zero, except for Boolean elements, which will be set to false .

See Also

System.Array Class, System Namespace

Array.Clone Method

public virtual object Clone();

Summary

Returns a Object that is a copy of the current instance.

Return Value

A Object that is a copy of the current instance.

Description

[Note: This method is implemented to support the ICloneable interface.]

[Behaviors: Each of the elements of the current instance is copied to the clone. If the elements are reference types, the references are copied. If the elements are value-types, the values are copied. The clone is of the same type as the current instance.]

[Default: As described above.]

[Overrides: Override this method to return a clone of an array.]

[Usage: Use this method to obtain the clone of an array.]

Example

This example demonstrates the System.Array.Clone method.

using System;
public class ArrayCloneExample {
  public static void Main() {
    int[] intAryOrig = { 3, 4, 5 };
    //must explicitly convert clones object into an array
    int[] intAryClone = (int[]) intAryOrig.Clone();
    Console.Write( "The elements of the first  array are: " );
    foreach( int i in intAryOrig )
      Console.Write( "{0,3}", i );
    Console.WriteLine();
    Console.Write( "The elements of the cloned array are: " );
    foreach( int i in intAryClone )
      Console.Write( "{0,3}", i );
    Console.WriteLine();
    //Clear the values of the original array.
    Array.Clear( intAryOrig, 0, 3 );
    Console.WriteLine( "After clearing the first array," );
    Console.Write( "The elements of the first  array are: " );
    foreach( int i in intAryOrig )
      Console.Write( "{0,3}", i );
    Console.WriteLine();
    Console.Write( "The elements of the cloned array are: " );
    foreach( int i in intAryClone )
      Console.Write( "{0,3}", i );
  }
}
   
The output is

The elements of the first array are: 3 4 5

The elements of the cloned array are: 3 4 5

After clearing the first array,

The elements of the first array are: 0 0 0

The elements of the cloned array are: 3 4 5

See Also

System.Array Class, System Namespace

Array.ConvertAll<T,U> Method

public static U[] ConvertAll<T,U>(T[] array, Converter<T,U> converter)

Summary

Converts an array of one type to an array of another type.

Parameters

array
The one-dimensional array to convert.
converter
A Converter<T,U> that converts each element from one type to another type.

Return Value

A new array of the target type containing the converted elements from array.

Exceptions

Exception TypeCondition
ArgumentNullExceptionarray is null or converter is null .

Description

The Converter<T,U> is a delegate that converts an array element to the target type. The elements of array are individually passed to this converter, and the converted elements are saved in the new array. The source array remains unchanged.

See Also

System.Array Class, System Namespace

Array.Copy(System.Array, System.Array, int) Method

public static void Copy(Array sourceArray, Array destinationArray, int length);

Summary

Copies the specified number of elements from the specified source array to the specified destination array.

Parameters

sourceArray
A Array that contains the data to copy.
destinationArray
A Array that receives the data.
length
A Int32 designating the number of elements to copy, starting with the first element and proceeding in order.

Exceptions

Exception TypeCondition
ArgumentNullExceptionsourceArray or destinationArray is null .

RankExceptionsourceArray and destinationArray have different ranks.

ArrayTypeMismatchExceptionThe elements in both arrays are built-in types, and converting from the type of the elements of sourceArray into the type of the elements in destinationArray requires a narrowing conversion.

-or-

Both arrays are built-in types, and one array is a value-type array and the other an array of interface type not implemented by that value-type.

-or-

Both arrays are user-defined value types and are not of the same type.

InvalidCastException At least one of the elements in sourceArray is not assignment-compatible with the type of destinationArray.

ArgumentOutOfRangeExceptionlength < 0.

ArgumentExceptionlength > sourceArray.Length.

-or-

length > destinationArray.Length.

Description

This version of System.Array.Copy(System.Array,System.Array,System.Int32) is equivalent to System.Array.Copy(System.Array,System.Array,System.Int32) (sourceArray, sourceArray.GetLowerBound(0), destinationArray, destinationArray.GetLowerBound(0), length).

If sourceArray and destinationArray are of different types, System.Array.Copy(System.Array,System.Array,System.Int32) performs widening conversions on the elements of sourceArray as necessary before storing the information in destinationArray. Value types will be boxed when being converted to a Object. If the necessary conversion is a narrowing conversion, a ArrayTypeMismatchException exception is thrown. [Note: For information regarding valid conversions performed by this method, see Convert.]

If an exception is thrown while copying, the state of destinationArray is undefined.

If sourceArray and destinationArray are the same array, System.Array.Copy(System.Array,System.Array,System.Int32) copies the source elements safely to their destination, as if the copy were done through an intermediate array.

Example

This example demonstrates the System.Array.Copy(System.Array,System.Array,System.Int32) method.

using System;
public class ArrayCopyExample {
   public static void Main() {
      int[] intAryOrig = new int[3];
      double[] dAryCopy = new double[3];
      for ( int i = 0; i < intAryOrig.Length; i++ )
         intAryOrig[i] = i+3;
      //copy the first 2 elements of the source into the destination
      Array.Copy( intAryOrig, dAryCopy, 2);
      Console.Write( "The elements of the first array are: " );
      for ( int i = 0; i < intAryOrig.Length; i++ ) 
         Console.Write( "{0,3}", intAryOrig[i] );
      Console.WriteLine();
      Console.Write( "The elements of the copied array are: " );
      for ( int i = 0; i < dAryCopy.Length; i++ ) 
         Console.Write( "{0,3}", dAryCopy[i] );
   }
}
   
The output is

The elements of the first array are: 3 4 5

The elements of the copied array are: 3 4 0

See Also

System.Array Class, System Namespace

Array.Copy(System.Array, int, System.Array, int, int) Method

public static void Copy(Array sourceArray, int sourceIndex, Array destinationArray, int destinationIndex, int length);

Summary

Copies the specified number of elements from a source array starting at the specified source index to a destination array starting at the specified destination index.

Parameters

sourceArray
The Array that contains the data to copy.
sourceIndex
A Int32 that contains the index in sourceArray from which copying begins.
destinationArray
The Array that receives the data.
destinationIndex
A Int32 that contains the index in destinationArray at which storing begins.
length
A Int32 that contains the number of elements to copy.

Exceptions

Exception TypeCondition
ArgumentNullExceptionsourceArray or destinationArray is null .

RankExceptionsourceArray and destinationArray have different ranks.

ArrayTypeMismatchExceptionThe elements in both arrays are built-in types, and converting from the type of the elements of sourceArray into the type of the elements in destinationArray requires a narrowing conversion.

-or-

Both arrays are built-in types, and one array is a value-type array and the other an array of interface type not implemented by that value-type.

-or-

Both arrays are user-defined value types and are not of the same type.

InvalidCastExceptionAt least one element in sourceArray is assignment-incompatible with the type of destinationArray.

ArgumentOutOfRangeExceptionsourceIndex < sourceArray.GetLowerBound(0).

-or-

destinationIndex < destinationArray.GetLowerBound(0).

-or-

length < 0.

ArgumentException(sourceIndex + length ) > (sourceArray.GetLowerBound(0) + sourceArray.Length).

(destinationIndex + length ) > ( destinationArray.GetLowerBound(0) + destinationArray.Length).

Description

If sourceArray and destinationArray are of different types, System.Array.Copy(System.Array,System.Array,System.Int32) performs widening conversions on the elements of sourceArray as necessary before storing the information in destinationArray. Value types will be boxed when being converted to a Object. If the necessary conversion is a narrowing conversion, a ArrayTypeMismatchException exception is thrown. [Note: For information regarding valid conversions performed by this method, see Convert .]

If an exception is thrown while copying, the state of destinationArray is undefined.

If sourceArray and destinationArray are the same array, System.Array.Copy(System.Array,System.Array,System.Int32) copies the source elements safely to their destination as if the copy were done through an intermediate array.

Example

This example demonstrates the System.Array.Copy(System.Array,System.Array,System.Int32) method.

using System;
class ArrayCopyExample {
   public static void Main() {
      int[] intAry = { 0, 10, 20, 30, 40, 50 };
      Console.Write( "The elements of the array are: " );
      foreach ( int i in intAry )
         Console.Write( "{0,3}", i );
      Console.WriteLine();
      Array.Copy( intAry, 2, intAry, 0, 4 );
      Console.WriteLine( "After copying elements 2 through 5 into elements 0 through 4" );
      Console.Write( "The elements of the array are: " );
      foreach ( int i in intAry )
         Console.Write( "{0,3}", i );
      Console.WriteLine();     
   }
}
   
The output is

The elements of the array are: 0 10 20 30 40 50

After copying elements 2 through 5 into elements 0 through 4

The elements of the array are: 20 30 40 50 40 50

See Also

System.Array Class, System Namespace

Array.CopyTo Method

public virtual void CopyTo(Array array, int index);

Summary

Copies all the elements of the current zero-based instance to the specified one-dimensional array starting at the specified subscript in the destination array.

Parameters

array
A one-dimensional Array that is the destination of the elements copied from the current instance.
index
A Int32 that contains the index in array at which copying begins.

Exceptions

Exception TypeCondition
ArgumentNullExceptionarray is null .
RankExceptionThe current instance has more than one dimension.

ArgumentOutOfRangeExceptionindex < array.GetLowerBound(0) .
ArgumentExceptionarray has more than one dimension.

-or-

( index + Length of the current instance) > (array.GetLowerBound(0) + array.Length ).

-or-

The number of elements in the current instance is greater than the available space from index to the end of array.

ArrayTypeMismatchExceptionThe element type of the current instance is not assignment-compatible with the element type of array.

Description

index is the array index in the destination array at which copying begins.

[Note: This method is implemented to support the ICollection interface. If implementing ICollection is not explicitly required, use System.Array.Copy(System.Array,System.Array,System.Int32) to avoid an extra indirection.

If this method throws an exception while copying, the state of array is undefined.

]

[Behaviors: As described above.]

[Default: As described above.]

[Overrides: Override this method to copy elements of the current instance to a specified array.]

[Usage: Use this method to copy elements of the current instance to a specified array.]

Example

The following example shows how to copy the elements of one Array into another.

using System;

public class ArrayCopyToExample
{
   public static void Main()
   {
      Array aryOne = Array.CreateInstance(typeof(Object), 3);
      aryOne.SetValue("one", 0);
      aryOne.SetValue("two", 1);
      aryOne.SetValue("three", 2);

      Array aryTwo = Array.CreateInstance(typeof(Object), 5);
      for (int i=0; i < aryTwo.Length; i++)
         aryTwo.SetValue(i, i);

      Console.WriteLine("The contents of the first array are:");
      foreach (object o in aryOne)
         Console.Write("{0} ", o);
      Console.WriteLine();
      Console.WriteLine("The original contents of the second array are:");
      foreach (object o in aryTwo)
         Console.Write("{0} ", o);
      Console.WriteLine();
      
      aryOne.CopyTo(aryTwo, 1);

      Console.WriteLine("The new contents of the second array are:");
      foreach( object o in aryTwo)
         Console.Write("{0} ", o);
   }
}
The output is

The contents of the first array are:

one two three

The original contents of the second array are:

0 1 2 3 4

The new contents of the second array are:

0 one two three 4

See Also

System.Array Class, System Namespace

Array.CreateInstance(System.Type, int[]) Method

public static Array CreateInstance(Type elementType, int[] lengths);

Summary

Creates a zero-based, multidimensional array of the specified Type and dimension lengths.

Parameters

elementType
The Type of the elements contained in the new Array instance.
lengths
A one-dimensional array of Int32 objects that contains the size of each dimension of the new Array instance.

Return Value

A new zero-based, multidimensional Array instance of the specified Type with the specified length for each dimension. The System.Array.Rank of the new instance is equal to lengths.Length.

Exceptions

Exception TypeCondition
ArgumentNullExceptionelementType or lengths is null .

ArgumentExceptionelementType is not a valid Type.

-or-

lengths.Length = 0.

ArgumentOutOfRangeExceptionA value in lengths is less than zero.

Description

The number of elements in lengths is required to equal the number of dimensions in the new Array instance. Each element of lengths specifies the length of the corresponding dimension in the new instance.

Reference-type elements will be set to null . Value-type elements will be set to zero, except for Boolean elements, which will be set to false .

[Note: Unlike most classes, Array provides the System.Array.CreateInstance(System.Type,System.Int32) method, instead of public constructors, to allow for late bound access.]

Example

The following example shows how to create and initialize a multidimensional Array.

using System;

public class CreateMultiDimArrayExample
{
   public static void Main()
   {
      int i, j, k;
      int[] indexAry = {2, 4, 5};
      Array ary = Array.CreateInstance( typeof(int), indexAry );
      for( i = ary.GetLowerBound(0); i <= ary.GetUpperBound(0); i++ )
      {
         for( j = ary.GetLowerBound(1); j <= ary.GetUpperBound(1); j++ )
         {
            for( k = ary.GetLowerBound(2); k <= ary.GetUpperBound(2); k++ )
            {
               ary.SetValue( (100*i + 10*j + k), i, j, k );
            }
         }
      }
      Console.WriteLine("The elements of the array are:");
      for( i = ary.GetLowerBound(0); i <= ary.GetUpperBound(0); i++)
      {
         for( j = ary.GetLowerBound(1); j <= ary.GetUpperBound(1); j++)
         {
             for( k = ary.GetLowerBound(2); k <= ary.GetUpperBound(2); k++ )
            {
               Console.Write("{0, 3} ", ary.GetValue(i, j, k));
            }
            Console.WriteLine();
         }
         Console.WriteLine();
      }
   }
}
   
The output is

The elements of the array are:
  0   1   2   3   4
 10  11  12  13  14
 20  21  22  23  24
 30  31  32  33  34

100 101 102 103 104
110 111 112 113 114
120 121 122 123 124
130 131 132 133 134 

Library

ExtendedArray

See Also

System.Array Class, System Namespace

Array.CreateInstance(System.Type, int, int, int) Method

public static Array CreateInstance(Type elementType, int length1, int length2, int length3);

Summary

Creates a zero-based, three-dimensional array of the specified Type and dimension lengths.

Parameters

elementType
The Type of the elements contained in the new Array instance.
length1
A Int32 that contains the number of elements contained in the first dimension of the new Array instance.
length2
A Int32 that contains the number of elements contained in the second dimension of the new Array instance.
length3
A Int32 that contains the number of elements contained in the third dimension of the new Array instance.

Return Value

A new zero-based, three-dimensional Array instance of elementType objects with the size length1 for the first dimension, length2 for the second, and length3 for the third.

Exceptions

Exception TypeCondition
ArgumentNullExceptionelementType is null .
ArgumentExceptionelementType is not a valid Type.
ArgumentOutOfRangeExceptionlength1 < 0.

-or-

length2 < 0.

-or-

length3 < 0.

Description

Reference-type elements will be set to null . Value-type elements will be set to zero, except for Boolean elements, which will be set to false .

[Note: Unlike most classes, Array provides the System.Array.CreateInstance(System.Type,System.Int32) method, instead of public constructors, to allow for late bound access.]

Example

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

using System;

public class Create3DArrayExample
{
   public static void Main()
   {
      int i, j, k;
      Array ary = Array.CreateInstance( typeof(int), 2, 4, 3 );
      for( i = ary.GetLowerBound(0); i <= ary.GetUpperBound(0); i++ )
      {
         for( j = ary.GetLowerBound(1); j <= ary.GetUpperBound(1); j++ )
         {
            for( k = ary.GetLowerBound(2); k <= ary.GetUpperBound(2); k++ )
            {
               ary.SetValue( (100*i + 10*j + k), i, j, k );
            }
         }
      }
      Console.WriteLine("The elements of the array are:");
      for( i = ary.GetLowerBound(0); i <= ary.GetUpperBound(0); i++)
      {
         for( j = ary.GetLowerBound(1); j <= ary.GetUpperBound(1); j++)
         {
             for( k = ary.GetLowerBound(2); k <= ary.GetUpperBound(2); k++ )
            {
               Console.Write("{0, 3} ", ary.GetValue(i, j, k));
            }
            Console.WriteLine();
         }
         Console.WriteLine();
      }
   }
}
   
The output is

The elements of the array are:
  0   1   2
 10  11  12
 20  21  22
 30  31  32

100 101 102
110 111 112
120 121 122
130 131 132
 

Library

ExtendedArray

See Also

System.Array Class, System Namespace

Array.CreateInstance(System.Type, int, int) Method

public static Array CreateInstance(Type elementType, int length1, int length2);

Summary

Creates a zero-based, two-dimensional array of the specified Type and dimension lengths.

Parameters

elementType
The Type of the elements contained in the new Array instance.
length1
A Int32 that contains the number of elements contained in the first dimension of the new Array instance.
length2
A Int32 that contains the number of elements contained in the second dimension of the new Array instance.

Return Value

A new zero-indexed, two-dimensional Array instance of elementType objects with the size length1 for the first dimension and length2 for the second.

Exceptions

Exception TypeCondition
ArgumentNullExceptionelementType is null .
ArgumentExceptionelementType is not a valid Type.
ArgumentOutOfRangeExceptionlength1 < 0.

-or-

length2 < 0.

Description

Reference-type elements will be set to null . Value-type elements will be set to zero, except for Boolean elements, which will be set to false .

[Note: Unlike most classes, Array provides the System.Array.CreateInstance(System.Type,System.Int32) method, instead of public constructors, to allow for late bound access.]

Example

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

using System;

public class Create2DArrayExample
{
   public static void Main()
   {
      int i, j;
      Array ary = Array.CreateInstance( typeof(int), 5, 3 );
      for( i = ary.GetLowerBound(0); i <= ary.GetUpperBound(0); i++ )
      {
         for( j = ary.GetLowerBound(1); j <= ary.GetUpperBound(1); j++ )
         {
            ary.SetValue( (10*i + j), i, j );
         }
      }
      Console.WriteLine("The elements of the array are:");
      for( i = ary.GetLowerBound(0); i <= ary.GetUpperBound(0); i++)
      {
         for( j = ary.GetLowerBound(1); j <= ary.GetUpperBound(1); j++)
         {
            Console.Write("{0, 2} ", ary.GetValue(i, j));
         }
         Console.WriteLine();
      }
   }
} 
  
The output is

The elements of the array are:
 0  1  2
10 11 12
20 21 22
30 31 32
40 41 42
 

Library

ExtendedArray

See Also

System.Array Class, System Namespace

Array.CreateInstance(System.Type, int) Method

public static Array CreateInstance(Type elementType, int length);

Summary

Constructs a zero-based, one-dimensional array with the specified number of elements of the specified type.

Parameters

elementType
The Type of the elements contained in the new Array instance.
length
A Int32 that contains the number of elements contained in the new Array instance.

Return Value

A zero-based, one-dimensional Array object containing length elements of type elementType.

Exceptions

Exception TypeCondition
ArgumentNullExceptionelementType is null .
ArgumentExceptionelementType is not a valid Type.
ArgumentOutOfRangeExceptionlength < 0.

Description

Reference-type elements will be set to null . Value-type elements will be set to zero, except for Boolean elements, which will be set to false .

[Note: Unlike most classes, Array provides the System.Array.CreateInstance(System.Type,System.Int32) method, instead of public constructors, to allow for late bound access.]

Example

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

using System;

public class ArrayCreateInstanceExample
{

   public static void Main()
   {

      Array intAry = Array.CreateInstance(typeof(int),5);
      for (int i=intAry.GetLowerBound(0);i<=intAry.GetUpperBound(0);i++)
         intAry.SetValue(i*3,i);
      Console.Write("The values of the array are:");
      foreach (int i in intAry)
         Console.Write("{0} ",i);
   
   }

}
   
The output is

The values of the array are: 0 3 6 9 12

See Also

System.Array Class, System Namespace

Array.CreateInstance(System.Type, int[], int[]) Method

public static Array CreateInstance(Type elementType, int[] lengths, int[] lowerBounds);

Summary

Creates a multidimensional array whose element type is the specified Type, and dimension lengths and lower bounds, as specified.

Parameters

elementType
The Type of the elements contained in the new Array instance.
lengths
A one-dimensional array of Int32 objects that contains the size of each dimension of the new Array instance.
lowerBounds
A one-dimensional array of Int32 objects that contains the lower bound of each dimension of the new Array instance.

Return Value

A new multidimensional Array whose element type is the specified Type and with the specified length and lower bound for each dimension.

Exceptions

Exception TypeCondition
ArgumentNullExceptionelementType, lengths, or lowerBounds is null .

ArgumentExceptionelementType is not a valid Type.

-or-

lengths.Length = 0.

-or-

lengths and lowerBounds do not contain the same number of elements.

ArgumentOutOfRangeExceptionA value in lengths is less than zero.

Description

The lengths and lowerBounds are required to have the same number of elements. The number of elements in lengths equals the number of dimensions in the new Array instance

Each element of lengths specifies the length of the corresponding dimension in the new Array instance.

Each element of lowerBounds specifies the lower bound of the corresponding dimension in the new Array instance.

Reference-type elements will be set to null . Value-type elements will be set to zero, except for Boolean elements, which will be set to false .

[Note: Unlike most classes, Array provides the System.Array.CreateInstance(System.Type,System.Int32) method, instead of public constructors, to allow for late bound access.]

Example

The following example shows how to create and initialize a multidimensional Array with specified low bounds.

using System;

public class MultiDimNonZeroBoundExample
{
   public static void Main()
   {
      int i, j, k;
      int[] indexAry = {4, 2, 3};
      int[] lowboundAry = {3, 2, 1};
      Array ary = Array.CreateInstance( typeof(int), indexAry, lowboundAry );
      for( i = ary.GetLowerBound(0); i <= ary.GetUpperBound(0); i++ )
      {
         for( j = ary.GetLowerBound(1); j <= ary.GetUpperBound(1); j++ )
         {
            for( k = ary.GetLowerBound(2); k <= ary.GetUpperBound(2); k++ )
            {
               ary.SetValue( (100*i + 10*j + k), i, j, k );
            }
         }
      }
      Console.WriteLine("The elements of the array are:");
      for( i = ary.GetLowerBound(0); i <= ary.GetUpperBound(0); i++)
      {
         for( j = ary.GetLowerBound(1); j <= ary.GetUpperBound(1); j++)
         {
             for( k = ary.GetLowerBound(2); k <= ary.GetUpperBound(2); k++ )
            {
               Console.Write("{0, 3} ", ary.GetValue(i, j, k));
            }
            Console.WriteLine();
         }
         Console.WriteLine();
      }
   }
}
   
The output is

The elements of the array are:
321 322 323
331 332 333

421 422 423
431 432 433

521 522 523
531 532 533

621 622 623
631 632 633

Library

ExtendedArray

See Also

System.Array Class, System Namespace

Array.Exists<T> Method

public static bool Exists<T>(T[] array, Predicate<T> match)

Summary

Determines whether the specified array contains any element that matches the conditions defined by the specified predicate.

Parameters

array
The array to search.
match
The predicate that defines the conditions of the elements to search for.

Return Value

true , if the array contains one or more elements that match the conditions defined by the specified predicate; otherwise, false .

Exceptions

Exception TypeCondition
ArgumentNullExceptionarray or match is null .

Description

The predicate returns true if the object passed to it matches the delegate. Each element of array is passed to the predicate in turn, and processing is stopped when the predicate returns true .

See Also

System.Array Class, System Namespace

Array.Find<T> Method

public static T Find<T>(T[] array, Predicate<T> match)

Summary

Searches for an element that matches the predicate, and returns the first occurrence within the entire array.

Parameters

array
The array to search.
match
The predicate that defines the conditions of the element to search for.

Return Value

The first element that matches the conditions defined by the specified predicate, if found; otherwise, the default value for type T.

Exceptions

Exception TypeCondition
ArgumentNullExceptionarray or match is null .

Description

The elements of array are individually passed to the predicate, moving forward in the array, starting with the first element and ending with the last element. Processing is stopped when the predicate returns true .

See Also

System.Array Class, System Namespace

Array.FindAll<T> Method

public static T[] FindAll<T>(T[] array, Predicate<T> match)

Summary

Retrieves all the elements that match the conditions defined by the specified predicate.

Parameters

array
The array to search.
match
The predicate that specifies the elements to search for.

Return Value

An array containing all the elements that match the conditions defined by the specified predicate, if found; otherwise, an empty array.

Exceptions

Exception TypeCondition
ArgumentNullExceptionarray or match is null .

Description

The elements of array are individually passed to the predicate, and those elements for which the predicate returns true , are saved in the returned array.

See Also

System.Array Class, System Namespace

Array.FindIndex<T>(T[], System.Predicate<T>) Method

public static int FindIndex<T>(T[] array, Predicate<T> match)

Summary

Searches for an element that matches the predicate, and returns the zero-based index of the first occurrence within the entire array.

Parameters

array
The array to search.
match
The predicate that specifies the elements to search for.

Return Value

The zero-based index of the first occurrence of an element that matches the conditions defined by match, if found; otherwise, -1.

Exceptions

Exception TypeCondition
ArgumentNullExceptionarray or match is null .

Description

The elements of array are individually passed to the predicate. The array is searched forward starting at the first element and ending at the last element. Processing is stopped when the predicate returns true .

See Also

System.Array Class, System Namespace

Array.FindIndex<T>(T[], int, System.Predicate<T>) Method

public static int FindIndex<T>(T[] array, int startIndex, Predicate<T> match)

Summary

Searches for an element that matches the 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.

Parameters

array
The array to search.
startIndex
The zero-based starting index of the search.

match
The predicate that specifies the elements to search for.

Return Value

The zero-based index of the first occurrence of an element that matches the conditions defined by match, if found; otherwise, -1.

Exceptions

Exception TypeCondition
ArgumentNullExceptionarray or match is null .
ArgumentOutOfRangeExceptionstartIndex is less than zero or greater than array.Length .

Description

The elements of array are individually passed to the predicate. The array is searched forward starting at the specified index and ending at the last element. Processing is stopped when the predicate returns true .

See Also

System.Array Class, System Namespace

Array.FindIndex<T>(T[], int, int, System.Predicate<T>) Method

public static int FindIndex<T>(T[] array, int startIndex, int count, Predicate<T> match)

Summary

Searches for an element that matches the 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.

Parameters

array
The array to search.
startIndex
The zero-based starting index of the search

count
The number of consecutive elements to search.

match
The predicate that specifies the elements to search for.

Return Value

The zero-based index of the first occurrence of an element that matches the conditions defined by match, if found; otherwise, -1.

Exceptions

Exception TypeCondition
ArgumentNullExceptionarray or match is null .
ArgumentOutOfRangeExceptionstartIndex is less than zero.

-or-

count is less than zero.

-or-

startIndex + count is greater than array.Length .

Description

The elements of array are individually passed to the predicate. The array is searched forward starting at the specified index and going for count elements. Processing is stopped when the predicate returns true .

See Also

System.Array Class, System Namespace

Array.FindLast<T> Method

public static T FindLast<T>(T[] array, Predicate<T> match)

Summary

Searches for an element that matches the predicate, and returns the last occurrence within the entire array.

Parameters

array
The array to search.
match
The predicate that specifies the elements to search for.

Return Value

The last element that matches the conditions defined by the specified predicate, if found; otherwise, the default value for type T.

Exceptions

Exception TypeCondition
ArgumentNullExceptionarray or match is null .

Description

The elements of array are individually passed to the predicate, moving backward in the array, starting with the last element and ending with the first element. Processing is stopped when a match is found.

See Also

System.Array Class, System Namespace

Array.FindLastIndex<T>(T[], System.Predicate<T>) Method

public static int FindLastIndex<T>(T[] array, Predicate<T> match)

Summary

Searches for an element that matches the predicate, and returns the zero-based index of the last occurrence within the entire array.

Parameters

array
The array to search.
match
The predicate that specifies the elements to search for.

Return Value

The zero-based index of the first occurrence of an element that matches the conditions defined by match, if found; otherwise, -1.

Exceptions

Exception TypeCondition
ArgumentNullExceptionarray or match is null .

Description

The elements of array are individually passed to the predicate. The array is searched backwards starting at the last element and ending at the first element. Processing is stopped when the predicate returns true .

See Also

System.Array Class, System Namespace

Array.FindLastIndex<T>(T[], int, System.Predicate<T>) Method

public static int FindLastIndex<T>(T[] array, int startIndex, Predicate<T> match)

Summary

Searches for an element that matches the predicate, and returns the zero-based index of the last occurrence within the range of elements in the array that extends from the specified index to the last element.

Parameters

array
The array to search.
startIndex
The zero-based starting index of the backward search.

match
The predicate that specifies the elements to search for.

Return Value

The zero-based index of the first occurrence of an element that matches the conditions defined by match, if found; otherwise, -1.

Exceptions

Exception TypeCondition
ArgumentNullExceptionarray or match is null .
ArgumentOutOfRangeExceptionstartIndex is less than zero or greater than array.Length .

Description

The elements of array are individually passed to the predicate. The array is searched backward starting at the specified index and ending at the first element. Processing is stopped when the predicate returns true .

See Also

System.Array Class, System Namespace

Array.FindLastIndex<T>(T[], int, int, System.Predicate<T>) Method

public static int FindLastIndex<T>(T[] array, int startIndex, int count, Predicate<T> match)

Summary

Searches for an element that matches the predicate, and returns the zero-based index of the last occurrence within the range of elements in the array that ends at the specified index and contains the specified number of elements.

Parameters

array
The array to search.
startIndex
The zero-based starting index of the backward search.

count
The number of consecutive elements to search.

match
The predicate that specifies the elements to search for.

Return Value

The zero-based index of the first occurrence of an element that matches the conditions defined by match, if found; otherwise, -1.

Exceptions

Exception TypeCondition
ArgumentNullExceptionarray or match is null .
ArgumentOutOfRangeExceptionstartIndex is less than zero or greater than array.Length .

-or-

count is less than zero.

-or-

count is greater than startIndex + 1.

Description

The elements of array are individually passed to the predicate. The array is searched backward starting at the specified index and going for count elements. Processing is stopped when the predicate returns true .

See Also

System.Array Class, System Namespace

Array.ForEach<T> Method

public static void ForEach<T>(T[] array, Action<T> action)

Summary

Performs the specified action on each element of the specified array.

Parameters

array
The array on whose elements the action is to be performed.
action
The action to perform on each element of array.

Return Value

The first element that matches the conditions defined by the specified predicate, if found; otherwise, the default value for type T.

Exceptions

Exception TypeCondition
ArgumentNullExceptionarray or action is null .

Description

The elements of array are individually passed to the action. The elements of the current array are individually passed to the action delegate, sequentially, in index order, and on the same thread as that used to call ForEach . Execution stops if the action throws an exception.

See Also

System.Array Class, System Namespace

Array.GetEnumerator Method

public virtual IEnumerator GetEnumerator();

Summary

Returns a IEnumerator for the current instance.

Return Value

A IEnumerator for the current instance.

Description

A IEnumerator grants read-access to the elements of a Array.

[Note: This method is implemented to support the IEnumerator interface. For more information regarding the use of an enumerator, see IEnumerator.]

[Behaviors: Enumerators can be used to read the data in the collection, but they cannot be used to modify the underlying collection.

Initially, the enumerator is positioned before the first element of the current instance. System.Collections.IEnumerator.Reset returns the enumerator to this position. Therefore, after an enumerator is created or after a System.Collections.IEnumerator.Reset, System.Collections.IEnumerator.MoveNext is required to be called to advance the enumerator to the first element of the collection before reading the value of System.Collections.IEnumerator.Current.

System.Collections.IEnumerator.Current returns the same object until either System.Collections.IEnumerator.MoveNext or System.Collections.IEnumerator.Reset is called. System.Collections.IEnumerator.MoveNext sets System.Collections.IEnumerator.Current to the next element.

If System.Collections.IEnumerator.MoveNext passes the end of the collection, the enumerator is positioned after the last element in the collection and System.Collections.IEnumerator.MoveNext returns false. When the enumerator is at this position, subsequent calls toSystem.Collections.IEnumerator.MoveNext also return false . If the last call to System.Collections.IEnumerator.MoveNext returned false , System.Collections.IEnumerator.Current is unspecified. To set System.Collections.IEnumerator.Current to the first element of the collection again, you can call System.Collections.IEnumerator.Reset followed by System.Collections.IEnumerator.MoveNext.

An enumerator remains valid as long as the collection remains unchanged. If changes are made to the collection, such as adding, modifying, or deleting elements, the enumerator is irrecoverably invalidated and its behavior is undefined.

The enumerator does not have exclusive access to the collection; therefore, enumerating through a collection is intrinsically not a thread safe procedure. To guarantee thread safety during enumeration, you can lock the collection during the entire enumeration. To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization.

]

[Default: Multidimensional arrays will be processed in Row-major form.

[Note: For some multidimensional Array objects, it can be desirable for an enumerator to process them in Column-major form.]

]

[Overrides: Override this method to provide read-access to the current instance.]

[Usage: Use this method to iterate over the elements of the current instance.]

Example

This example demonstrates the System.Array.GetEnumerator method.

using System;
using System.Collections;
public class ArrayGetEnumerator {
   public static void Main() {
      string[,] strAry = {{"1","one"}, {"2", "two"}, {"3", "three"}};
      Console.Write( "The elements of the array are: " );
      IEnumerator sEnum = strAry.GetEnumerator();
      while ( sEnum.MoveNext() )
         Console.Write( " {0}", sEnum.Current );
   }
}
   
The output is

The elements of the array are: 1 one 2 two 3 three

See Also

System.Array Class, System Namespace

Array.GetLength Method

public int GetLength(int dimension)

Summary

Gets the number of elements in the specified dimension of the array.

Parameters

dimension
The zero-based dimension of the array whose length is to be determined.

Return Value

The number of elements in the specified dimension of the array.

Exceptions

Exception TypeCondition
IndexOutOfRangeExceptiondimension is less than zero.

-or-

dimension is equal to or greater than System.Array.Rank.

Library

RuntimeInfrastructure

See Also

System.Array Class, System Namespace

Array.GetLowerBound Method

public int GetLowerBound(int dimension);

Summary

Returns the lower bound of the specified dimension in the current instance.

Parameters

dimension
A Int32 that contains the zero-based dimension of the current instance whose lower bound is to be determined.

Return Value

A Int32 that contains the lower bound of the specified dimension in the current instance.

Exceptions

Exception TypeCondition
IndexOutOfRangeExceptiondimension < 0.

-or-

dimension is equal to or greater than the System.Array.Rank property of the current instance.

Description

[Note: For example, System.Array.GetLowerBound(System.Int32) (0) returns the lower bound of the first dimension of the current instance, and System.Array.GetLowerBound(System.Int32)(System.Array.Rank - 1) returns the lower bound of the last dimension of the current instance.]

Library

RuntimeInfrastructure

See Also

System.Array Class, System Namespace

Array.GetUpperBound Method

public int GetUpperBound(int dimension);

Summary

Returns the upper bound of the specified dimension in the current instance.

Parameters

dimension
A Int32 that contains the zero-based dimension of the current instance whose upper bound is to be determined.

Return Value

A Int32 that contains the upper bound of the specified dimension in the current instance.

Exceptions

Exception TypeCondition
IndexOutOfRangeExceptiondimension < 0.

-or-

dimension is equal to or greater than the System.Array.Rank property of the current instance.

Description

[Note: For example, System.Array.GetUpperBound(System.Int32) (0) returns the upper bound of the first dimension of the current instance, and System.Array.GetUpperBound(System.Int32)(System.Array.Rank - 1) returns the upper bound of the last dimension of the current instance.]

Library

RuntimeInfrastructure

See Also

System.Array Class, System Namespace

Array.GetValue(int[]) Method

public object GetValue(int[] indices);

Summary

Gets the value at the specified position in the current multidimensional instance.

Parameters

indices
A one-dimensional array of Int32 objects that contains the indices that specify the position of the element in the current instance whose value to get.

Return Value

A Object that contains the value at the specified position in the current instance.

Exceptions

Exception TypeCondition
ArgumentNullExceptionindices is null .
ArgumentExceptionThe number of dimensions in the current instance is not equal to the number of elements in indices.
IndexOutOfRangeExceptionAt least one element in indices is outside the range of valid indices for the corresponding dimension of the current instance.

Description

The number of elements in indices is required to be equal to the number of dimensions in the current instance. All elements in indices collectively specify the position of the desired element in the current instance.

[Note: Use the System.Array.GetLowerBound(System.Int32) and System.Array.GetUpperBound(System.Int32) methods to determine whether any of the values in indices are out of bounds.]

Library

ExtendedArray

See Also

System.Array Class, System Namespace

Array.GetValue(int) Method

public object GetValue(int index);

Summary

Gets the value at the specified position in the current one-dimensional instance.

Parameters

index
A Int32 that contains the position of the value to get from the current instance.

Return Value

A Object that contains the value at the specified position in the current instance.

Exceptions

Exception TypeCondition
ArgumentExceptionThe current instance has more than one dimension.
IndexOutOfRangeExceptionindex is outside the range of valid indices for the current instance.

Description

[Note: Use the System.Array.GetLowerBound(System.Int32) and System.Array.GetUpperBound(System.Int32) methods to determine whether index is out of bounds.]

Example

This example demonstrates the System.Array.GetValue(System.Int32[]) method.

using System;
public class ArrayGetValueExample {
   public static void Main() {
      String[] strAry = { "one", "two", "three", "four", "five" };
      Console.Write( "The elements of the array are: " );
      for( int i = 0; i < strAry.Length; i++ )
         Console.Write( " '{0}' ", strAry.GetValue( i ) );
   }
}
   
The output is

The elements of the array are: 'one' 'two' 'three' 'four' 'five'

See Also

System.Array Class, System Namespace

Array.GetValue(int, int) Method

public object GetValue(int index1, int index2);

Summary

Gets the value at the specified position in the current two-dimensional instance.

Parameters

index1
A Int32 that contains the first-dimension index of the element in the current instance to get.
index2
A Int32 that contains the second-dimension index of the element in the current instance to get.

Return Value

A Object that contains the value at the specified position in the current instance.

Exceptions

Exception TypeCondition
ArgumentExceptionThe current instance does not have exactly two dimensions.
IndexOutOfRangeExceptionAt least one of index1 or index2 is outside the range of valid indexes for the corresponding dimension of the current instance.

Description

[Note: Use the System.Array.GetLowerBound(System.Int32) and System.Array.GetUpperBound(System.Int32) methods to determine whether any of the indices are out of bounds.]

Library

ExtendedArray

See Also

System.Array Class, System Namespace

Array.GetValue(int, int, int) Method

public object GetValue(int index1, int index2, int index3);

Summary

Gets the value at the specified position in the current three-dimensional instance.

Parameters

index1
A Int32 that contains the first-dimension index of the element in the current instance to get.
index2
A Int32 that contains the second-dimension index of the element in the current instance to get.
index3
A Int32 that contains the third-dimension index of the element in the current instance to get.

Return Value

A Object that contains the value at the specified position in the current instance.

Exceptions

Exception TypeCondition
ArgumentExceptionThe current instance does not have exactly three dimensions.
IndexOutOfRangeExceptionAt least one ofindex1 or index2 or index3 is outside the range of valid indexes for the corresponding dimension of the current instance.

Description

[Note: Use the System.Array.GetLowerBound(System.Int32) and System.Array.GetUpperBound(System.Int32) methods to determine whether any of the indices are out of bounds.]

Library

ExtendedArray

See Also

System.Array Class, System Namespace

Array.IndexOf(System.Array, System.Object, int, int) Method

public static int IndexOf(Array array, object value, int startIndex, int count);

Summary

Searches the specified one-dimensional Array, returning the index of the first occurrence of the specified Object in the specified range.

Parameters

array
A one-dimensional Array to search.
value
A Object to locate in array.
startIndex
A Int32 that contains the index at which searching starts.
count
A Int32 that contains the number of elements to search, beginning with startIndex.

Return Value

A Int32 containing the index of the first occurrence of value in array, within the range startIndex through startIndex + count- 1, if found; otherwise, array.GetLowerBound(0) - 1. [Note: For a vector, if value is not found, the return value will be -1. This provides the caller with a standard code for the failed search.]

Exceptions

Exception TypeCondition
ArgumentNullExceptionarray is null .

ArgumentOutOfRangeExceptionstartIndex is less than array.GetLowerBound(0) .

-or-

count is less than zero.

-or-

startIndex + count is greater than array.GetLowerBound(0) + array.Length .

RankExceptionarray has more than one dimension.

Description

The elements are compared using System.Object.Equals(System.Object).

See Also

System.Array Class, System Namespace

Array.IndexOf(System.Array, System.Object, int) Method

public static int IndexOf(Array array, object value, int startIndex);

Summary

Searches the specified one-dimensional Array, returning the index of the first occurrence of the specified Object between the specified index and the last element.

Parameters

array
A one-dimensional Array to search.
value
A Object to locate in array.
startIndex
A Int32 that contains the index at which searching starts.

Return Value

A Int32 containing the index of the first occurrence of value in array, within the range startIndex through the last element of array, if found; otherwise, array.GetLowerBound(0) - 1. [Note: For a vector, if value is not found, the return value will be -1. This provides the caller with a standard code for the failed search.]

Exceptions

Exception TypeCondition
ArgumentNullExceptionarray is null .

ArgumentOutOfRangeExceptionstartIndex is less than array.GetLowerBound(0) or greater than array.GetLowerBound(0) + array.Length .

RankExceptionarray has more than one dimension.

Description

This version of System.Array.IndexOf(System.Array,System.Object) is equivalent to System.Array.IndexOf(System.Array,System.Object) (array, value , startIndex, (array.Length - startIndex+array.GetLowerBound(0))).

The elements are compared using System.Object.Equals(System.Object).

See Also

System.Array Class, System Namespace

Array.IndexOf(System.Array, System.Object) Method

public static int IndexOf(Array array, object value);

Summary

Searches the specified one-dimensional Array, returning the index of the first occurrence of the specified Object.

Parameters

array
A one-dimensional Array to search.
value
A Object to locate in array.

Return Value

A Int32 containing the index of the first occurrence of value in array, if found; otherwise, array.GetLowerBound(0) - 1. [Note: For a vector, if value is not found, the return value will be -1. This provides the caller with a standard code for a failed search.]

Exceptions

Exception TypeCondition
ArgumentNullExceptionarray is null .
RankExceptionarray has more than one dimension.

Description

This version of System.Array.IndexOf(System.Array,System.Object) is equivalent to System.Array.IndexOf(System.Array,System.Object)(array, value, array.GetLowerBound(0),array.Length).

The elements are compared using System.Object.Equals(System.Object).

Example

The following example demonstrates the System.Array.IndexOf(System.Array,System.Object) method.

using System;
public class ArrayIndexOfExample {
   public static void Main() {
      int[] intAry = { 0, 1, 2, 0, 1 };
      Console.Write( "The values of the array are: " );
      foreach( int i in intAry )
         Console.Write( "{0,5}", i );
      Console.WriteLine();
      int j = Array.IndexOf( intAry, 1 );
      Console.WriteLine( "The first occurrence of 1 is at index {0}", j );
   }
}
The output is

The values of the array are: 0 1 2 0 1

The first occurrence of 1 is at index 1

See Also

System.Array Class, System Namespace

Array.IndexOf<T>(T[], T, int, int) Method

public static int IndexOf<T>(T[] array, T value, int startIndex, int count)

Summary

Searches for the specified value and returns the index of the first occurrence within the range of elements in the array starting at the specified index and continuing for, at most, the specified number of elements.

Parameters

array
The array to search.
value
The value to locate.
startIndex
The zero-based starting index of the search.
count
The number of consecutive elements to search.

Return Value

The zero-based index of the first occurrence of value within the range of elements in array that starts at startIndex and contains the number of elements specified in count , if found; otherwise, -1.

Exceptions

Exception TypeCondition
ArgumentNullExceptionarray is null .

ArgumentOutOfRangeExceptionstartIndex is less than zero.

-or-

count is less than zero.

-or-

startIndex + count is greater than System.Array.Length.

Description

The elements are compared using System.Object.Equals(System.Object). The array is searched forward starting at startIndex and ending at startIndex + count - 1. Processing is stopped when the predicate returns true .

See Also

System.Array Class, System Namespace

Array.IndexOf<T>(T[], T, int) Method

public static int IndexOf<T>(T[] array, T value, int startIndex)

Summary

Searches the specified array, returning the index of the first occurrence in the specified array starting at the specified index and including the last element.

Parameters

array
The array to search.
value
The value to locate.
startIndex
The zero-based starting index of the search.

Return Value

The zero-based index of the first occurrence of value within the range of elements in array that extends from startIndex to the last element, if found; otherwise, -1. If startIndex is equal to the length of the array, -1 is returned.

Exceptions

Exception TypeCondition
ArgumentNullExceptionarray is null .

ArgumentOutOfRangeExceptionstartIndex is less than zero or greater than array.Length .

Description

The elements are compared using System.Object.Equals(System.Object). The array is searched forward starting at startIndex and ending at the last element. Processing is stopped when the predicate returns true .

See Also

System.Array Class, System Namespace

Array.IndexOf<T>(T[], T) Method

public static int IndexOf<T>(T[] array, T value)

Summary

Searches the specified array, returning the index of the first occurrence of the specified value.

Parameters

array
The array to search.
value
The value to locate.

Return Value

The zero-based index of the first occurrence of value in array, if found; otherwise, - 1.

Exceptions

Exception TypeCondition
ArgumentNullExceptionarray is null .

Description

The elements are compared using System.Object.Equals(System.Object). The array is searched forward starting at the first element and ending at the last element. Processing is stopped when the predicate returns true .

See Also

System.Array Class, System Namespace

Array.Initialize Method

public void Initialize();

Summary

Initializes every element of the current instance of value-type objects by calling the default constructor of that value type.

Description

This method cannot be used on reference-type arrays.

If the current instance is not a value-type Array or if the value type does not have a default constructor, the current instance is not modified.

The current instance can have any lower bound and any number of dimensions.

[Note: This method can be used only on value types that have constructors. ]

Library

RuntimeInfrastructure

See Also

System.Array Class, System Namespace

Array.LastIndexOf(System.Array, System.Object, int, int) Method

public static int LastIndexOf(Array array, object value, int startIndex, int count);

Summary

Searches the specified one-dimensional Array, returning the index of the last occurrence of the specified Object in the specified range.

Parameters

array
A one-dimensional Array to search.
value
A Object to locate in array.
startIndex
A Int32 that contains the index at which searching starts.
count
A Int32 that contains the number of elements to search, beginning with startIndex .

Return Value

A Int32 containing the index of the last occurrence of value in array, within the range startIndex through startIndex - count + 1, if found; otherwise, array.GetLowerBound(0) - 1. [Note: For a vector, if value is not found, the return value will be -1. This provides the caller with a standard code for the failed search.]

Exceptions

Exception TypeCondition
ArgumentNullExceptionarray is null .

ArgumentOutOfRangeExceptionstartIndex is outside the range of valid indices for array.

-or-

count < 0.

-or-

count is greater than startIndex + 1.

RankExceptionarray has more than one dimension.

Description

The elements are compared using System.Object.Equals(System.Object).

See Also

System.Array Class, System Namespace

Array.LastIndexOf(System.Array, System.Object, int) Method

public static int LastIndexOf(Array array, object value, int startIndex);

Summary

Searches the specified one-dimensional Array, returning the index of the last occurrence of the specified Object between the specified index and the first element.

Parameters

array
A one-dimensional Array to search.
value
A Object to locate in array.
startIndex
A Int32 that contains the index at which searching starts.

Return Value

A Int32 containing the index of the last occurrence of value in the range startIndex through the lower bound of array, if found; otherwise, array.GetLowerBound(0) - 1. [Note: For a vector, if value is not found, the return value will be -1. This provides the caller with a standard code for the failed search.]

Exceptions

Exception TypeCondition
ArgumentNullExceptionarray is null .

ArgumentOutOfRangeExceptionstartIndex is outside the range of valid indices for array.

RankExceptionarray has more than one dimension.

Description

This version of System.Array.LastIndexOf(System.Array,System.Object) is equivalent to System.Array.LastIndexOf(System.Array,System.Object)( array, value, startIndex,startIndex+ 1 -array.GetLowerBound(0)).

The elements are compared using System.Object.Equals(System.Object).

See Also

System.Array Class, System Namespace

Array.LastIndexOf(System.Array, System.Object) Method

public static int LastIndexOf(Array array, object value);

Summary

Searches the specified one-dimensional Array, returning the index of the last occurrence of the specified Object.

Parameters

array
A one-dimensional Array to search.
value
A Object to locate in array.

Return Value

A Int32 containing the index of the last occurrence in array of value, if found; otherwise, array.GetLowerBound(0) - 1. [Note: For a vector, if value is not found, the return value will be -1. This provides the caller with a standard code for the failed search.]

Exceptions

Exception TypeCondition
ArgumentNullExceptionarray is null .
RankExceptionarray has more than one dimension.

Description

This version of System.Array.LastIndexOf(System.Array,System.Object) is equivalent to System.Array.LastIndexOf(System.Array,System.Object)(array, value, (array.GetLowerBound(0) + array.Length - 1), array.Length).

The elements are compared using System.Object.Equals(System.Object).

Example

The following example demonstrates the System.Array.LastIndexOf(System.Array,System.Object) method.

using System;

public class ArrayLastIndexOfExample {

   public static void Main() {
      int[] intAry = { 0, 1, 2, 0, 1 };
      Console.Write( "The values of the array are: ");
      foreach( int i in intAry )
         Console.Write( "{0,5}", i );
      Console.WriteLine();
      int j = Array.LastIndexOf( intAry, 1 );
      Console.WriteLine( "The last occurrence of 1 is at index {0}", j );
   }
}
The output is

The values of the array are: 0 1 2 0 1

The last occurrence of 1 is at index 4

See Also

System.Array Class, System Namespace

Array.LastIndexOf<T>(T[], T, int, int) Method

public static int LastIndexOf<T>(T[] array, T value, int startIndex, int count)

Summary

Searches for the specified value and returns the index of the last occurrence within the range of elements in the array starting at the specified index and continuing backwards for, at most, the specified number of elements.

Parameters

array
The array to search.
value
The value to locate.
startIndex
The zero-based starting index of the search.
count
The number of consecutive elements to search.

Return Value

The zero-based index of the last occurrence of value within the range of elements in array that ends at startIndex and contains the number of elements specified in count , if found; otherwise, -1.

Exceptions

Exception TypeCondition
ArgumentNullExceptionarray is null .

ArgumentOutOfRangeExceptionstartIndex is outside the range of valid indices for array.

-or-

count is less than zero.

-or-

count is greater than startIndex + 1.

Description

The elements are compared using System.Object.Equals(System.Object). The array is searched backward starting at startIndex and going for count elements. Processing is stopped when the predicate returns true .

See Also

System.Array Class, System Namespace

Array.LastIndexOf<T>(T[], T, int) Method

public static int LastIndexOf<T>(T[] array, T value, int startIndex)

Summary

Searches the specified array backwards, returning the index of the last occurrence of the specified array, starting at the specified index.

Parameters

array
The array to search.
value
The value to locate.
startIndex
The zero-based starting index of the search.

Return Value

The zero-based index of the last occurrence of value within the range of elements in array that extends from startIndex to the first element, if found; otherwise, -1.

Exceptions

Exception TypeCondition
ArgumentNullExceptionarray is null .

ArgumentOutOfRangeExceptionstartIndex is outside the range of valid indices for array.

Description

The elements are compared using System.Object.Equals(System.Object). The array is searched backward starting at startIndex and ending at the first element. Processing is stopped when the predicate returns true .

See Also

System.Array Class, System Namespace

Array.LastIndexOf<T>(T[], T) Method

public static int LastIndexOf<T>(T[] array, T value)

Summary

Searches the specified array, returning the index of the last occurrence of the specified value.

Parameters

array
The array to search.
value
The value to locate.

Return Value

The zero-based index of the last occurrence of value in array, if found; otherwise, - 1.

Exceptions

Exception TypeCondition
ArgumentNullExceptionarray is null .

Description

The elements are compared using System.Object.Equals(System.Object). The array is searched backward starting at the last element and ending at the first element. Processing is stopped when the predicate returns true .

See Also

System.Array Class, System Namespace

Array.Resize<T> Method

public static void Resize<T>(ref T[] array, int newSize)

Summary

Changes the size of an array to the specified new size.

Parameters

array
The array to resize.

-or-

null to create a new array with the specified size.

newSize
The size of the new array.

Exceptions

Exception TypeCondition
ArgumentOutOfRangeExceptionnewSize is less than zero.

Description

If array is null , this method creates a new array with the specified size.

If array is not null , then if newSize is equal to System.Array.Length of the old array, this method does nothing. Otherwise, this method allocates a new array with the specified size, copies elements from the old array to the new one, and then assigns the new array reference to the array parameter. If newSize is greater than System.Array.Length of the old array, a new array is allocated and all the elements are copied from the old array to the new one. If newSize is less than System.Array.Length of the old array, a new array is allocated and elements are copied from the old array to the new one until the new one is filled; the rest of the elements in the old array are ignored.

See Also

System.Array Class, System Namespace

Array.Reverse(System.Array, int, int) Method

public static void Reverse(Array array, int index, int length);

Summary

Reverses the sequence of the elements in the specified range of the specified one-dimensional Array.

Parameters

array
The one-dimensional Array to reverse.
index
A Int32 that contains the index at which reversing starts.
length
A Int32 that contains the number of elements to reverse.

Exceptions

Exception TypeCondition
ArgumentNullExceptionarray is null .
RankExceptionarray is multidimensional.
ArgumentOutOfRangeExceptionindex < array.GetLowerBound(0).

length < 0.

ArgumentExceptionindex and length do not specify a valid range in array (i.e. index + length > array.GetLowerBound(0) + array.Length).

Example

The following example demonstrates the System.Array.Reverse(System.Array) method.

using System;
public class ArrayReverseExample {
   public static void Main() {
      string[] strAry = { "one", "two", "three" };
      Console.Write( "The elements of the array are:");
      foreach( string str in strAry )
         Console.Write( " {0}", str );
      Array.Reverse( strAry );
      Console.WriteLine();
      Console.WriteLine( "After reversing the array," );
      Console.Write( "the elements of the array are:");
      foreach( string str in strAry )
         Console.Write( " {0}", str );
   }
}
The output is

The elements of the array are: one two three

After reversing the array,

the elements of the array are: three two one

See Also

System.Array Class, System Namespace

Array.Reverse(System.Array) Method

public static void Reverse(Array array);

Summary

Reverses the sequence of the elements in the specified one-dimensional Array.

Parameters

array
The one-dimensional Array to reverse.

Exceptions

Exception TypeCondition
ArgumentNullExceptionarray is null .
RankExceptionarray has more than one dimension.

Description

This version of System.Array.Reverse(System.Array) is equivalent to System.Array.Reverse(System.Array)(array, array.GetLowerBound(0), array.Length).

See Also

System.Array Class, System Namespace

Array.SetValue(System.Object, int) Method

public void SetValue(object value, int index);

Summary

Sets the value of the element at the specified position in the current one-dimensional instance.

Parameters

value
A Object that contains the new value for the specified element.
index
A Int32 that contains the index of the element whose value is to be set.

Exceptions

Exception TypeCondition
ArgumentExceptionThe current instance has more than one dimension.

IndexOutOfRangeExceptionindex is outside the range of valid indices for the current instance.

InvalidCastExceptionvalue is not assignment-compatible with the element type of the current instance.

Description

[Note: Use the System.Array.GetLowerBound(System.Int32) and System.Array.GetUpperBound(System.Int32) methods to determine whether index is out of bounds.

For more information regarding valid conversions that will be performed by this method, see Convert.

]

See Also

System.Array Class, System Namespace

Array.SetValue(System.Object, int, int) Method

public void SetValue(object value, int index1, int index2);

Summary

Sets the value of the element at the specified position in the current two-dimensional instance.

Parameters

value
A Object that contains the new value for the specified element.
index1
A Int32 that contains the first-dimension index of the element in the current instance to set.
index2
A Int32 that contains the second-dimension index of the element in the current instance to set.

Exceptions

Exception TypeCondition
ArgumentExceptionThe current instance does not have exactly two dimensions.

IndexOutOfRangeExceptionAt least one of index1 or index2 is outside the range of valid indices for the corresponding dimension of the current instance.
InvalidCastExceptionvalue is not assignment-compatible with the element type of the current instance.

Description

[Note: For more information regarding valid conversions that will be performed by this method, see Convert.

Use the System.Array.GetLowerBound(System.Int32) and System.Array.GetUpperBound(System.Int32) methods to determine whether any of the indices are out of bounds.

]

Library

ExtendedArray

See Also

System.Array Class, System Namespace

Array.SetValue(System.Object, int, int, int) Method

public void SetValue(object value, int index1, int index2, int index3);

Summary

Sets the value of the element at the specified position in the current three-dimensional instance.

Parameters

value
A Object that contains the new value for the specified element.
index1
A Int32 that contains the first-dimension index of the element in the current instance to set.
index2
A Int32 that contains the second-dimension index of the element in the current instance to set.
index3
A Int32 that contains the third-dimension index of the element in the current instance to set.

Exceptions

Exception TypeCondition
ArgumentExceptionThe current instance does not have exactly three dimensions.

IndexOutOfRangeExceptionAt least one of index1, index2, or index3 is outside the range of valid indices for the corresponding dimension of the current instance.
InvalidCastExceptionvalue is not assignment-compatible with the element type of the current instance.

Description

[Note: For more information regarding valid conversions that will be performed by this method, see Convert.

Use the System.Array.GetLowerBound(System.Int32) and System.Array.GetUpperBound(System.Int32) methods to determine whether any of the indices are out of bounds.

]

Library

ExtendedArray

See Also

System.Array Class, System Namespace

Array.SetValue(System.Object, int[]) Method

public void SetValue(object value, int[] indices);

Summary

Sets the value of the element at the specified position in the current multidimensional instance.

Parameters

value
A Object that contains the new value for the specified element.
indices
A one-dimensional array of Int32 objects that contains the indices that specify the position of the element in the current instance to set.

Exceptions

Exception TypeCondition
ArgumentNullExceptionindices is null .
ArgumentExceptionThe number of dimensions in the current instance is not equal to the number of elements in indices.

IndexOutOfRangeExceptionAt least one element in indices is outside the range of valid indices for the corresponding dimension of the current instance.

InvalidCastExceptionvalue is not assignment-compatible with the element type of the current instance.

Description

The number of elements in indices is required to be equal to the number of dimensions in the current instance. All elements in indices collectively specify the position of the desired element in the current instance.

[Note: For more information regarding valid conversions that will be performed by this method, see Convert.

Use the System.Array.GetLowerBound(System.Int32) and System.Array.GetUpperBound(System.Int32) methods to determine whether any of the values in indices is out of bounds.

]

Library

ExtendedArray

See Also

System.Array Class, System Namespace

Array.Sort(System.Array, System.Array, int, int, System.Collections.IComparer) Method

public static void Sort(Array keys, Array items, int index, int length, IComparer comparer);

Summary

Sorts the specified range of the specified pair of one-dimensional Array objects (one containing a set of keys and the other containing corresponding items) based on the keys in the first specified Array using the specified IComparer implementation.

Parameters

keys
A one-dimensional Array that contains the keys to sort.
items
A one-dimensional Array that contains the items that correspond to each element of keys. Specify a null reference to sort only keys.

index
A Int32 that contains the index at which sorting starts.
length
A Int32 that contains the number of elements to sort.
comparer
The IComparer implementation to use when comparing elements. Specify a null reference to use the IComparable implementation of each element.

Exceptions

Exception TypeCondition
ArgumentNullExceptionkeys is null .

RankExceptionkeys has more than one dimension.

-or-

items is not a null reference and has more than one dimension.

ArgumentOutOfRangeExceptionindex < keys.GetLowerBound(0).

-or-

length < 0.

ArgumentExceptionitems is not a null reference, and keys.GetLowerBound(0) does not equal items.GetLowerBound(0).

-or-

index and length do not specify a valid range in key.

-or-

items is not a null reference, and index and length do not specify a valid range in items.

InvalidOperationExceptioncomparer is null , and one or more elements in keys that are used in a comparison do not implement the IComparable interface.

Description

Each key in keys is required to have a corresponding item in items. The sort is performed according to the order of keys. After a key is repositioned during the sort, the corresponding item in items is similarly repositioned. Only keys.Length elements of items will be sorted. Therefore, items is sorted according to the arrangement of the corresponding keys in keys. If the sort is not successfully completed, the results are undefined.

If comparer is a null reference, each element of keys is required to implement the IComparable interface to be capable of comparisons with every other element in keys.

See Also

System.Array Class, System Namespace

Array.Sort(System.Array, int, int, System.Collections.IComparer) Method

public static void Sort(Array array, int index, int length, IComparer comparer);

Summary

Sorts the elements in the specified section of the specified one-dimensional Array using the specified IComparer implementation.

Parameters

array
A one-dimensional Array to sort.
index
A Int32 that contains the index at which sorting starts.
length
A Int32 that contains the number of elements to sort.
comparer
The IComparer implementation to use when comparing elements. Specify a null reference to use the IComparable implementation of each element.

Exceptions

Exception TypeCondition
ArgumentNullExceptionarray is null .
RankExceptionarray has more than one dimension.
ArgumentOutOfRangeExceptionindex < array.GetLowerBound(0).

-or-

length < 0.

ArgumentExceptionindex and length do not specify a valid range in array.

InvalidOperationExceptioncomparer is null , and one or more elements in array that are used in a comparison do not implement the IComparable interface.

Description

This version of System.Array.Sort(System.Array) is equivalent to System.Array.Sort(System.Array)(array, null , index, length, comparer).

If comparer is a null reference, each element of array is required to implement the IComparable interface to be capable of comparisons with every other element in array. If the sort is not successfully completed, the results are unspecified.

See Also

System.Array Class, System Namespace

Array.Sort(System.Array, System.Array, System.Collections.IComparer) Method

public static void Sort(Array keys, Array items, IComparer comparer);

Summary

Sorts the specified pair of one-dimensional Array objects (one containing a set of keys and the other containing corresponding items) based on the keys in the first specified Array using the specified IComparer implementation.

Parameters

keys
A one-dimensional Array that contains the keys to sort.
items
A one-dimensional Array that contains the items that correspond to each element in keys. Specify a null reference to sort only keys.

comparer
The IComparer implementation to use when comparing elements. Specify a null reference to use the IComparable implementation of each element.

Exceptions

Exception TypeCondition
ArgumentNullExceptionkeys is null .

RankExceptionkeys has more than one dimension.

-or-

items is not a null reference and has more than one dimension.

ArgumentExceptionitems is not a null reference, and keys.GetLowerBound(0) does not equal items.GetLowerBound(0).

-or-

items is not a null reference, and keys.Length > items.Length.

InvalidOperationExceptioncomparer is a null , and one or more elements in keys that are used in a comparison do not implement the IComparable interface.

Description

This version of System.Array.Sort(System.Array) is equivalent to System.Array.Sort(System.Array)(keys, items, keys.GetLowerBound(0), keys.Length, comparer).

Each key in keys is required to have a corresponding item in items. The sort is performed according to the order of keys . After a key is repositioned during the sort, the corresponding item in items is similarly repositioned. Only keys.Length elements of items are sorted. Therefore, items is sorted according to the arrangement of the corresponding keys in keys. If the sort is not successfully completed, the results are unspecified.

If comparer is a null reference, each element of keys is required to implement the IComparable interface to be capable of comparisons with every other element in keys.

See Also

System.Array Class, System Namespace

Array.Sort(System.Array, System.Collections.IComparer) Method

public static void Sort(Array array, IComparer comparer);

Summary

Sorts the elements in the specified one-dimensional Array using the specified IComparer implementation.

Parameters

array
The one-dimensional Array to sort.
comparer
The IComparer implementation to use when comparing elements. Specify a null reference to use the IComparable implementation of each element.

Exceptions

Exception TypeCondition
ArgumentNullExceptionarray is null .
RankExceptionarray has more than one dimension.
InvalidOperationExceptioncomparer is a null reference, and one or more elements in array that are used in a comparison do not implement the IComparable interface.

Description

This version of System.Array.Sort(System.Array) is equivalent to System.Array.Sort(System.Array)(array, null , array.GetLowerBound(0), array.Length, comparer).

If comparer is a null reference, each element of array is required to implement the IComparable interface to be capable of comparisons with every other element in array. If the sort is not successfully completed, the results are unspecified.

See Also

System.Array Class, System Namespace

Array.Sort(System.Array, System.Array, int, int) Method

public static void Sort(Array keys, Array items, int index, int length);

Summary

Sorts the specified ranges of the specified pair of one-dimensional Array objects (one containing a set of keys and the other containing corresponding items) based on the keys in the first specified Array.

Parameters

keys
A one-dimensional Array that contains the keys to sort.
items
A one-dimensional Array that contains the items that correspond to each element in keys. Specify a null reference to sort only keys.
index
A Int32 that contains the index at which sort begins.
length
A Int32 that contains the number of elements to sort.

Exceptions

Exception TypeCondition
ArgumentNullExceptionkeys is null .

RankExceptionkeys has more than one dimension.

-or-

items is not a null reference and has more than one dimension.

ArgumentOutOfRangeExceptionindex < keys.GetLowerBound(0).

-or-

length < 0.

ArgumentExceptionitems is not a null reference, and keys.GetLowerBound(0) does not equal items.GetLowerBound(0).

-or-

index and length do not specify a valid range in keys.

-or-

items is not a null reference, and index and length do not specify a valid range in items.

InvalidOperationExceptionOne or more elements in keys that are used in a comparison do not implement the IComparable interface.

Description

This version of System.Array.Sort(System.Array) is equivalent to System.Array.Sort(System.Array)(keys, items, index, length, null ).

Each key in keys is required to have a corresponding item in items. The sort is performed according to the order of keys . After a key is repositioned during the sort, the corresponding item in items is similarly repositioned. Therefore, items is sorted according to the arrangement of the corresponding keys in keys. If the sort is not successfully completed, the results are undefined.

Each element of keys is required to implement the IComparable interface to be capable of comparisons with every other element in keys.

See Also

System.Array Class, System Namespace

Array.Sort(System.Array) Method

public static void Sort(Array array);

Summary

Sorts the elements of the specified one-dimensional Array.

Parameters

array
A one-dimensional Array to sort.

Exceptions

Exception TypeCondition
ArgumentNullExceptionarray is null .
RankExceptionarray has more than one dimension.
InvalidOperationExceptionOne or more elements in array that are used in a comparison do not implement the IComparable interface.

Description

This version of System.Array.Sort(System.Array) is equivalent to System.Array.Sort(System.Array)(array, null , array.GetLowerBound(0), array.Length, null ).

Each element of array is required to implement the IComparable interface to be capable of comparisons with every other element in array.

Example

This example demonstrates the System.Array.Sort(System.Array) method.

using System;
public class ArraySortExample {
   public static void Main() {
      string[] strAry = { "All's", "well", "that", "ends", "well" };
      Console.Write( "The original string array is: " );
      foreach ( String str in strAry )
         Console.Write( str + " " );
      Console.WriteLine();
      Array.Sort( strAry );
      Console.Write( "The sorted string array is: " );
      foreach ( string str in strAry )
         Console.Write( str + " " );
   }
}
The output is

The original string array is: All's well that ends well

The sorted string array is: All's ends that well well

See Also

System.Array Class, System Namespace

Array.Sort(System.Array, System.Array) Method

public static void Sort(Array keys, Array items);

Summary

Sorts the specified pair of one-dimensional Array objects (one containing a set of keys and the other containing corresponding items) based on the keys in the first specified Array.

Parameters

keys
A one-dimensional Array that contains the keys to sort.
items
A one-dimensional Array that contains the items that correspond to each of element of keys. Specify a null reference to sort only keys.

Exceptions

Exception TypeCondition
ArgumentNullExceptionkeys is null .

RankExceptionkeys has more than one dimension.

-or-

items is not a null reference and has more than one dimension.

ArgumentExceptionitems is not a null reference, and keys.GetLowerBound(0) does not equal items.GetLowerBound(0).

-or-

items is not a null reference, and keys.Length > items.Length.

InvalidOperationExceptionOne or more elements in keys that are used in a comparison do not implement the IComparable interface.

Description

This version of System.Array.Sort(System.Array) is equivalent to System.Array.Sort(System.Array)(keys, items, keys.GetLowerBound(0), keys.Length, null ).

Each key in keys is required to have a corresponding item in items. The sort is performed according to the order of keys . After a key is repositioned during the sort, the corresponding item in items is similarly repositioned. Only keys.Length elements of items are sorted. Therefore, items is sorted according to the arrangement of the corresponding keys in keys. If the sort is not successfully completed, the results are unspecified.

Each element of keys is required to implement the IComparable interface to be capable of comparisons with every other element in keys.

Example

This example demonstrates the System.Array.Sort(System.Array) method.

using System;
public class ArraySortExample {
   public static void Main() {
      string[] strAry = { "All's", "well", "that", "ends", "well" };
      int[] intAry = { 3, 4, 0, 1, 2 };
      Console.Write( "The original string array is: " );
      foreach ( string str in strAry )
         Console.Write( str + " " );
      Console.WriteLine();
      Console.Write( "The key array is: " );
      foreach ( int i in intAry )
         Console.Write( i + " " );
      Console.WriteLine();
      Array.Sort( intAry, strAry );
      Console.Write( "The sorted string array is: " );
      foreach ( string str in strAry )
         Console.Write( str + " " );
   }
}
The output is

The original string array is: All's well that ends well

The key array is: 3 4 0 1 2

The sorted string array is: that ends well All's well

See Also

System.Array Class, System Namespace

Array.Sort(System.Array, int, int) Method

public static void Sort(Array array, int index, int length);

Summary

Sorts the elements in the specified range of the specified one-dimensional Array.

Parameters

array
A one-dimensional Array to sort.
index
A Int32 that contains the index at which sorting starts.
length
A Int32 that contains the number of elements to sort.

Exceptions

Exception TypeCondition
ArgumentNullExceptionarray is null .
RankExceptionarray has more than one dimension.
ArgumentOutOfRangeExceptionindex < array.GetLowerBound(0).

-or-

length < 0.

ArgumentExceptionindex and length do not specify a valid range in array.

InvalidOperationExceptionOne or more elements in array that are used in a comparison do not implement the IComparable interface.

Description

This version of System.Array.Sort(System.Array) is equivalent to System.Array.Sort(System.Array)(array, null , index, length, null ).

Each element of array is required to implement the IComparable interface to be capable of comparisons with every other element in array. If the sort is not successfully completed, the results are unspecified.

See Also

System.Array Class, System Namespace

Array.Sort<K,V>(K[], V[], int, int, System.Collections.Generic.IComparer<K>) Method

public static void Sort<K,V>(K[] keys, V[] items, int index, int length, IComparer<K> comparer)

Summary

Sorts a range of elements in a pair of arrays based on the keys in the first array using the specified System.Collections.Generic.IComparer<K>.

Parameters

keys
The array that contains the keys to sort.

items
The array that contains the items that correspond to each of the keys in keys.

-or-

null to sort only the keys array.

index
The starting index of the range to sort.
length
The number of elements in the range to sort.
comparer
The System.Collections.Generic.IComparer<K> implementation to use when comparing elements.

-or-

null to use the System.IComparable<K> or IComparable implementation of each element.

Exceptions

Exception TypeCondition
ArgumentExceptionindex and length do not specify a valid range in keys.

-or-

items is not null , and index and length do not specify a valid range in items.

ArgumentNullExceptionkeys is null .

ArgumentOutOfRangeExceptionindex is less than zero.

-or-

length is less than zero.

InvalidOperationExceptioncomparer is null , and one or more elements in keys that are used in a comparison do not implement the System.IComparable<K> or System.IComparable interface.

Description

If items is non-null, each key in keys is required to have a corresponding item in items. The sort is performed according to the order of keys. After a key is repositioned during the sort, the corresponding item in items is similarly repositioned. Only keys.Length elements of items will be sorted. Therefore, items is sorted according to the arrangement of the corresponding keys in keys. If the sort is not successfully completed, the results are undefined.

If comparer is a null reference, each element of keys is required to implement the System.IComparable<K> or IComparable interface to be capable of comparisons with every other element in keys.

See Also

System.Array Class, System Namespace

Array.Sort<K,V>(K[], V[], System.Collections.Generic.IComparer<K>) Method

public static void Sort<K,V>(K[] keys, V[] items, IComparer<K> comparer)

Summary

Sorts a pair of arrays based on the keys in the first array, using the specified System.Collections.Generic.IComparer.

Parameters

keys
The array that contains the keys to sort.

items
The array that contains the items that correspond to each of the keys in keys.

-or-

null to sort only the keys array.

comparer
The System.Collections.Generic.IComparer<K> implementation to use when comparing elements.

-or-

null to use the System.IComparable<K> or IComparable implementation of each element.

Exceptions

Exception TypeCondition
ArgumentNullExceptionkeys is null .

ArgumentExceptionitems is not null , and the length of keys does not match the length of items.

InvalidOperationExceptioncomparer is null , and one or more elements in keys that are used in a comparison do not implement the System.IComparable<K> or System.IComparable interface.

Description

This version of System.Array.Sort is equivalent to System.Array.Sort<K,V>( keys, items, 0, keys.Length , comparer) .

If items is non-null, each key in keys is required to have a corresponding item in items. The sort is performed according to the order of keys. After a key is repositioned during the sort, the corresponding item in items is similarly repositioned. Only keys.Length elements of items will be sorted. Therefore, items is sorted according to the arrangement of the corresponding keys in keys. If the sort is not successfully completed, the results are unspecified.

If comparer is a null reference, each element of keys is required to implement the System.IComparable<K> or IComparable interface to be capable of comparisons with every other element in keys.

See Also

System.Array Class, System Namespace

Array.Sort<K,V>(K[], V[], int, int) Method

public static void Sort<K,V>(K[] keys, V[] items, int index, int length)

Summary

Sorts a range of elements in a pair of arrays based on the keys in the first array, using the System.IComparable<K> or IComparable implementation of each key.

Parameters

keys
The array that contains the keys to sort.

items
The array that contains the items that correspond to each of the keys in keys.

-or-

null to sort only the keys array.

index
The starting index of the range to sort.
length
The number of elements in the range to sort.

Exceptions

Exception TypeCondition
ArgumentExceptionindex and length do not specify a valid range in keys.

-or-

items is not null , and index and length do not specify a valid range in items.

ArgumentNullExceptionkeys is null .

ArgumentOutOfRangeExceptionindex is less than zero.

-or-

length is less than zero.

InvalidOperationExceptionOne or more elements in keys that are used in a comparison are the null reference or do not implement the System.IComparable<K> or System.IComparable interface.

Description

If items is non-null, each key in keys is required to have a corresponding item in items. When a key is repositioned during the sorting, the corresponding item in items is similarly repositioned. Therefore, items is sorted according to the arrangement of the corresponding keys in keys.

If the sort is not successfully completed, the results are unspecified.

Each key within the specified range of elements in keys must implement the System.IComparable<K> or IComparable interface to be capable of comparisons with every other key.

This implementation performs an unstable sort; that is, if two elements are equal, their order might not be preserved. In contrast, a stable sort preserves the order of elements that are equal.

See Also

System.Array Class, System Namespace

Array.Sort<K,V>(K[], V[]) Method

public static void Sort<K,V>(K[] keys, V[] items)

Summary

Sorts a pair of arrays based on the keys in the first array using the IComparable implementation of each key.

Parameters

keys
The array that contains the keys to sort.

items
The array that contains the items that correspond to each of the keys in keys.

-or-

null to sort only the keys array.

Exceptions

Exception TypeCondition
ArgumentExceptionitems is not null , and the length of keys does not equal the length of items.

ArgumentNullExceptionkeys is null .

InvalidOperationExceptionOne or more elements in keys that are used in a comparison are the null reference or do not implement the System.IComparable<K> or System.IComparable interface.

Description

If items is non-null, each key in keys is required to have a corresponding item in items. When a key is repositioned during the sorting, the corresponding item in items is similarly repositioned. Therefore, items is sorted according to the arrangement of the corresponding keys in keys.

Each key in keys must implement the System.IComparable<K> or IComparable interface to be capable of comparisons with every other key.

If the sort is not successfully completed, the results are undefined.

This implementation performs an unstable sort; that is, if two elements are equal, their order might not be preserved. In contrast, a stable sort preserves the order of elements that are equal.

See Also

System.Array Class, System Namespace

Array.Sort<T>(T[], int, int, System.Collections.Generic.IComparer<T>) Method

public static void Sort<T>(T[] array, int index, int length, IComparer<T> comparer)

Summary

Sorts the elements in a range of elements in an array using the specified comparer.

Parameters

array
The array to sort.

index
The starting index of the range to sort.
length
The number of elements in the range to sort.
comparer
The System.Collections.Generic.IComparer<K> implementation to use when comparing elements.

-or-

null to use the System.IComparable<K> or IComparable implementation of each element.

Exceptions

Exception TypeCondition
ArgumentNullExceptionarray is null .

ArgumentOutOfRangeExceptionindex is less than zero.

-or-

length is less than zero.

ArgumentExceptionindex and length do not specify a valid range in array.

InvalidOperationExceptioncomparer is null , and one or more elements in array that are used in a comparison do not implement the System.IComparable<K> or System.IComparable interface.

Description

If comparer is null, each element within the specified range of elements in array must implement the IComparable interface to be capable of comparisons with every other element in array.

If the sort is not successfully completed, the results are undefined.

This implementation performs an unstable sort; that is, if two elements are equal, their order might not be preserved. In contrast, a stable sort preserves the order of elements that are equal.

See Also

System.Array Class, System Namespace

Array.Sort<T>(T[], System.Collections.Generic.IComparer<T>) Method

public static void Sort<T>(T[] array, IComparer<T> comparer)

Summary

Sorts the elements in an array using the specified comparer.

Parameters

array
The array to sort.

comparer
The IComparer<T> implementation to use when comparing elements.

-or-

null to use the IComparable<T> or IComparable implementation of each element.

Exceptions

Exception TypeCondition
ArgumentNullExceptionarray is null .

InvalidOperationExceptioncomparer is null , and one or more elements in array that are used in a comparison do not implement the IComparable<T> or System.IComparable interface.

Description

If comparer is null, each element of array must implement the IComparable<T> or IComparable interface to be capable of comparisons with every other element in array.

If the sort is not successfully completed, the results are undefined.

This implementation performs an unstable sort; that is, if two elements are equal, their order might not be preserved. In contrast, a stable sort preserves the order of elements that are equal.

See Also

System.Array Class, System Namespace

Array.Sort<T>(T[], System.Comparison<T>) Method

public static void Sort<T>(T[] array, Comparison<T> comparison)

Summary

Sorts the elements in an array using the specified comparison.

Parameters

array
The array to sort.

comparison
The Comparison<T> to use when comparing elements.

Exceptions

Exception TypeCondition
ArgumentNullExceptionarray is null .

-or-

comparison is null .

Description

If the sort is not successfully completed, the results are undefined.

This implementation performs an unstable sort; that is, if two elements are equal, their order might not be preserved. In contrast, a stable sort preserves the order of elements that are equal.

See Also

System.Array Class, System Namespace

Array.Sort<T>(T[]) Method

public static void Sort<T>(T[] array)

Summary

Sorts the elements in an entire array using the IComparable<T> or IComparable implementation of each element of that array.

Parameters

array
The array to sort.

Exceptions

Exception TypeCondition
ArgumentNullExceptionarray is null .

InvalidOperationExceptionOne or more elements in array that are used in a comparison are the null reference or do not implement the IComparable<T> or System.IComparable interface.

Description

Each element of array is required to implement the IComparable<T> or IComparable interface to be capable of comparisons with every other element in array.

If the sort is not successfully completed, the results are undefined.

This implementation performs an unstable sort; that is, if two elements are equal, their order might not be preserved. In contrast, a stable sort preserves the order of elements that are equal.

See Also

System.Array Class, System Namespace

Array.Sort<T>(T[], int, int) Method

public static void Sort<T>(T[] array, int index, int length)

Summary

Sorts an array using the IComparable<T> or IComparable implementation of each element of that array.

Parameters

array
The array to sort.

index
The starting index of the range to sort.
length
The number of elements in the range to sort.

Exceptions

Exception TypeCondition
ArgumentExceptionindex and length do not specify a valid range in array.

ArgumentNullExceptionarray is null .

ArgumentOutOfRangeExceptionindex is less than zero.

-or-

length is less than zero.

InvalidOperationExceptionOne or more elements in array that are used in a comparison do not implement the IComparable<T> or System.IComparable interface.

Description

Each element within the specified range of elements in array must implement the IComparable<T> or IComparable interface to be capable of comparisons with every other element in array.

If the sort is not successfully completed, the results are undefined.

This implementation performs an unstable sort; that is, if two elements are equal, their order might not be preserved. In contrast, a stable sort preserves the order of elements that are equal.

See Also

System.Array Class, System Namespace

Array.System.Collections.IList.Add Method

int IList.Add(object value);

Summary

Implemented to support the IList interface. [Note: For more information, see System.Collections.IList.Add.]

See Also

System.Array Class, System Namespace

Array.System.Collections.IList.Clear Method

void IList.Clear();

Summary

Implemented to support the IList interface. [Note: For more information, see System.Collections.IList.Clear.]

See Also

System.Array Class, System Namespace

Array.System.Collections.IList.Contains Method

bool IList.Contains(object value);

Summary

Implemented to support the IList interface. [Note: For more information, see System.Collections.IList.Contains.]

See Also

System.Array Class, System Namespace

Array.System.Collections.IList.IndexOf Method

int IList.IndexOf(object value);

Summary

Implemented to support the IList interface. [Note: For more information, see System.Collections.IList.IndexOf.]

See Also

System.Array Class, System Namespace

Array.System.Collections.IList.Insert Method

void IList.Insert(int index, object value);

Summary

Implemented to support the IList interface. [Note: For more information, see System.Collections.IList.Insert.]

See Also

System.Array Class, System Namespace

Array.System.Collections.IList.Remove Method

void IList.Remove(object value);

Summary

Implemented to support the IList interface. [Note: For more information, see System.Collections.IList.Remove.]

See Also

System.Array Class, System Namespace

Array.System.Collections.IList.RemoveAt Method

void IList.RemoveAt(int index);

Summary

Implemented to support the IList interface. [Note: For more information, see System.Collections.IList.RemoveAt.]

See Also

System.Array Class, System Namespace

Array.TrueForAll<T> Method

public static bool TrueForAll<T>(T[] array, Predicate<T> match)

Summary

Determines whether every element in the array matches the predicate.

Parameters

array
The array to check against the conditions.
match
The predicate against which the elements are checked..

Return Value

true , if every element in array matches the specified predicate; otherwise, false .

Exceptions

Exception TypeCondition
ArgumentNullExceptionarray or match is null .

Description

The predicate returns true if the object passed to it matches the delegate. The elements of array are individually passed to the predicate, and processing is stopped when the delegate returns false for any element.

See Also

System.Array Class, System Namespace

Array.IsFixedSize Property

public bool IsFixedSize { get; }

Summary

Implemented to support the IList interface. [Note: For more information, see System.Collections.IList.IsFixedSize.]

See Also

System.Array Class, System Namespace

Array.IsReadOnly Property

public bool IsReadOnly { get; }

Summary

Implemented to support the IList interface. [Note: For more information, see System.Collections.IList.IsReadOnly.]

See Also

System.Array Class, System Namespace

Array.IsSynchronized Property

public bool IsSynchronized { get; }

Summary

Implemented to support the ICollection interface. [Note: For more information, see System.Collections.ICollection.IsSynchronized.]

See Also

System.Array Class, System Namespace

Array.Length Property

public int Length { get; }

Summary

Gets the total number of elements in all the dimensions of the current instance.

Property Value

A Int32 that contains the total number of elements in all the dimensions of the current instance.

Description

This property is read-only.

See Also

System.Array Class, System Namespace

Array.LongLength Property

public long LongLength {get;}

Summary

Gets the total number of elements in all the dimensions of the current instance.

Property Value

A Int64 value containing the length of the array.

Description

This property is read-only.

See Also

System.Array Class, System Namespace

Array.Rank Property

public int Rank { get; }

Summary

Gets the rank (number of dimensions) of the current instance.

Property Value

A Int32 that contains the rank (number of dimensions) of the current instance.

Description

This property is read-only.

See Also

System.Array Class, System Namespace

Array.SyncRoot Property

public object SyncRoot { get; }

Summary

Implemented to support the ICollection interface. [Note: For more information, see System.Collections.ICollection.SyncRoot.]

See Also

System.Array Class, System Namespace

Array.System.Collections.ICollection.Count Property

int ICollection.Count { get; }

Summary

Implemented to support the ICollection interface. [Note: For more information, see System.Collections.ICollection.Count.]

See Also

System.Array Class, System Namespace

Array.System.Collections.IList.Item Property

public virtual object this[int index] { get; set; }

Summary

Implemented to support the IList interface. [Note: For more information, see System.Collections.IList.Item.]

See Also

System.Array Class, System Namespace