public abstract class Array : ICloneable, ICollection, IEnumerable, IList
Object
ArrayThis type implements ICloneable, ICollection, IEnumerable, and IList.
mscorlib
BCL
Serves as the base class for arrays. Provides methods for creating, copying, manipulating, searching, and sorting arrays.
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
Get
: Takes as many Int32 arguments as the array has dimensions and returns the value stored at the given index. It throws a IndexOutOfRangeException exception for invalid indices.
Set
: Takes as many Int32 arguments as the array has dimensions, plus one additional argument (the last argument) which has the same type as an array element. It stores the final value in the specified index of the array. It throws a IndexOutOfRangeException exception for invalid indices.
Address
: Takes as many Int32 arguments as the array has dimensions and returns the address of the element at the given index. It throws a IndexOutOfRangeException exception for invalid indices.
(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.
System Namespace
Array Constructors
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
private Array();
Constructs a new instance of the Array class.
System.Array Class, System Namespace
public static IList<T> AsReadOnly<T>(T[] array)
Returns a read-only System.Collections.Generic.IList<T> wrapper around the specified array.
- array
- The array to wrap in a read-only IList<T> wrapper.
A read-only IList<T> wrapper around the specified array.
Exception Type Condition ArgumentNullException array is null
.
[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.
System.Array Class, System Namespace
public static int BinarySearch(Array array, int index, int length, object value, IComparer comparer);
Searches the specified section of the specified one-dimensional Array for the specified value, using the specified IComparer implementation.
- 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.
A Int32 with one of the following values based on the result of the search operation.
[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.]
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 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.
Exception Type Condition ArgumentNullException array is null
.RankException array has more than one dimension. ArgumentOutOfRangeException index is less than array .GetLowerBound(0)
.-or-
length is less than zero.
ArgumentException index + length is greater than array .GetLowerBound(0)
+ array.Length
.-or-
array.UpperBound == System.Int32.MaxValue.
InvalidOperationException comparer is null
, and both value and at least one element of array do not implement the IComparable interface.
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 isnull
, 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.]
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.
System.Array Class, System Namespace
public static int BinarySearch(Array array, object value, IComparer comparer);
Searches the specified one-dimensional Array for the specified value, using the specified IComparer implementation.
- 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.
A Int32 with one of the following values based on the result of the search operation.
[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.]
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 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.
Exception Type Condition ArgumentNullException array is null
.RankException array has more than one dimension. InvalidOperationException comparer is null
, and both value and at least one element of array do not implement the IComparable interface.
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.]
System.Array Class, System Namespace
public static int BinarySearch(Array array, int index, int length, object value);
Searches the specified section of the specified one-dimensional Array for the specified value.
- 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.
A Int32 with one of the following values based on the result of the search operation.
[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.]
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 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.
Exception Type Condition ArgumentNullException array is null
.RankException array has more than one dimension. ArgumentOutOfRangeException index < array.GetLowerBound(0). -or-
length < 0.
ArgumentException index 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.
InvalidOperationException Either value or at least one element of array does not implement the IComparable interface.
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.]
System.Array Class, System Namespace
public static int BinarySearch(Array array, object value);
Searches the specified one-dimensional Array for the specified object.
- array
- A Array to search for an object.
- value
- A Object for which to search.
A Int32 with one of the following values based on the result of the search operation.
[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.]
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.
Exception Type Condition ArgumentNullException array is null
.RankException array has more than one dimension.
InvalidOperationException Both value and at least one element of array do not implement the IComparable interface.
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.]
System.Array Class, System Namespace
public static int BinarySearch<T>(T[] array, T value)
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.
- array
- The one-dimensional array to search.
- value
- The object for which to search.
One of the following values based on the result of the search operation:
Return Value Description 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.
Exception Type Condition ArgumentNullException array is null
.InvalidOperationException Neither value nor the elements of the array implement the IComparable<T> or IComparable interfaces.
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 unspecifiedDuplicate 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 withnull
do not generate an exception.]
System.Array Class, System Namespace
public static int BinarySearch<T>(T[] array, T value, IComparer<T> comparer)
Searches an entire one-dimensional sorted array for a value using the specified IComparer<T> interface.
- 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.
One of the following values based on the result of the search operation:
Return Value Description 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.
Exception Type Condition ArgumentNullException array is null
.InvalidOperationException comparer is null
, and neither value nor the elements of the array implement the IComparable<T> or IComparable interface.
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 withnull
do not generate an exception.]
System.Array Class, System Namespace
public static int BinarySearch<T>(T[] array, int index, int length, T value)
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.
- 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.
One of the following values based on the result of the search operation:
Return Value Description 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.
Exception Type Condition ArgumentException index + length is greater than array .Length
.
ArgumentNullException array is null
.ArgumentOutOfRangeException index is less than zero -or-
length is less than zero.
InvalidOperationException Neither value nor the elements of the array implement the IComparable<T> or IComparable interface.
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 unspecifiedDuplicate 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 withnull
do not generate an exception.]
System.Array Class, System Namespace
public static int BinarySearch<T>(T[] array, int index, int length, T value, IComparer<T> comparer)
Searches a range of elements in a one-dimensional sorted array for a value, using the specified IComparer<T> interface.
- 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.
One of the following values based on the result of the search operation:
Return Value Description 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.
Exception Type Condition ArgumentException index and length do not specify a valid range in array.
ArgumentNullException array is null
.ArgumentOutOfRangeException index is less than zero -or-
length is less than zero.
InvalidOperationException comparer is null
, and neither value nor the elements of the array implement the IComparable<T> or IComparable interface.
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 withnull
do not generate an exception.]
System.Array Class, System Namespace
public static void Clear(Array array, int index, int length);
Sets the specified range of elements in the specified Array to zero, false, or to a null reference, depending on the element type.
- 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.
Exception Type Condition ArgumentNullException array is null
.ArgumentOutOfRangeException index < 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 ).
Reference-type elements will be set tonull
. Value-type elements will be set to zero, except for Boolean elements, which will be set tofalse
.
System.Array Class, System Namespace
public virtual object Clone();
Returns a Object that is a copy of the current instance.
A Object that is a copy of the current instance.
[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.]
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
System.Array Class, System Namespace
public static U[] ConvertAll<T,U>(T[] array, Converter<T,U> converter)
Converts an array of one type to an array of another type.
- array
- The one-dimensional array to convert.
- converter
- A Converter<T,U> that converts each element from one type to another type.
A new array of the target type containing the converted elements from array.
Exception Type Condition ArgumentNullException array is null
or converter isnull
.
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.
System.Array Class, System Namespace
public static void Copy(Array sourceArray, Array destinationArray, int length);
Copies the specified number of elements from the specified source array to the specified destination array.
- 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.
Exception Type Condition ArgumentNullException sourceArray or destinationArray is null
.
RankException sourceArray and destinationArray have different ranks.
ArrayTypeMismatchException The 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.
ArgumentOutOfRangeException length < 0.
ArgumentException length > sourceArray.Length. -or-
length > destinationArray.Length.
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.
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
System.Array Class, System Namespace
public static void Copy(Array sourceArray, int sourceIndex, Array destinationArray, int destinationIndex, int length);
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.
- 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.
Exception Type Condition ArgumentNullException sourceArray or destinationArray is null
.
RankException sourceArray and destinationArray have different ranks.
ArrayTypeMismatchException The 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 element in sourceArray is assignment-incompatible with the type of destinationArray.
ArgumentOutOfRangeException sourceIndex < 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).
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.
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
System.Array Class, System Namespace
public virtual void CopyTo(Array array, int index);
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.
- 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.
Exception Type Condition ArgumentNullException array is null
.RankException The current instance has more than one dimension.
ArgumentOutOfRangeException index < array .GetLowerBound(0)
.ArgumentException array 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.
ArrayTypeMismatchException The element type of the current instance is not assignment-compatible with the element type of array.
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.]
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
System.Array Class, System Namespace
public static Array CreateInstance(Type elementType, int[] lengths);
Creates a zero-based, multidimensional array of the specified Type and dimension lengths.
- 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.
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.
Exception Type Condition ArgumentNullException elementType or lengths is null
.
ArgumentException elementType is not a valid Type. -or-
lengths.Length = 0.
ArgumentOutOfRangeException A value in lengths is less than zero.
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 tofalse
.[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.]
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
ExtendedArray
System.Array Class, System Namespace
public static Array CreateInstance(Type elementType, int length1, int length2, int length3);
Creates a zero-based, three-dimensional array of the specified Type and dimension lengths.
- 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.
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.
Exception Type Condition ArgumentNullException elementType is null
.ArgumentException elementType is not a valid Type. ArgumentOutOfRangeException length1 < 0. -or-
length2 < 0.
-or-
length3 < 0.
Reference-type elements will be set tonull
. Value-type elements will be set to zero, except for Boolean elements, which will be set tofalse
.[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.]
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
ExtendedArray
System.Array Class, System Namespace
public static Array CreateInstance(Type elementType, int length1, int length2);
Creates a zero-based, two-dimensional array of the specified Type and dimension lengths.
- 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.
A new zero-indexed, two-dimensional Array instance of elementType objects with the size length1 for the first dimension and length2 for the second.
Exception Type Condition ArgumentNullException elementType is null
.ArgumentException elementType is not a valid Type. ArgumentOutOfRangeException length1 < 0. -or-
length2 < 0.
Reference-type elements will be set tonull
. Value-type elements will be set to zero, except for Boolean elements, which will be set tofalse
.[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.]
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
ExtendedArray
System.Array Class, System Namespace
public static Array CreateInstance(Type elementType, int length);
Constructs a zero-based, one-dimensional array with the specified number of elements of the specified type.
- 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.
A zero-based, one-dimensional Array object containing length elements of type elementType.
Exception Type Condition ArgumentNullException elementType is null
.ArgumentException elementType is not a valid Type. ArgumentOutOfRangeException length < 0.
Reference-type elements will be set tonull
. Value-type elements will be set to zero, except for Boolean elements, which will be set tofalse
.[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.]
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
System.Array Class, System Namespace
public static Array CreateInstance(Type elementType, int[] lengths, int[] lowerBounds);
Creates a multidimensional array whose element type is the specified Type, and dimension lengths and lower bounds, as specified.
- 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.
A new multidimensional Array whose element type is the specified Type and with the specified length and lower bound for each dimension.
Exception Type Condition ArgumentNullException elementType, lengths, or lowerBounds is null
.
ArgumentException elementType is not a valid Type. -or-
lengths.Length = 0.
-or-
lengths and lowerBounds do not contain the same number of elements.
ArgumentOutOfRangeException A value in lengths is less than zero.
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 instanceEach 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 tofalse
.[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.]
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
ExtendedArray
System.Array Class, System Namespace
public static bool Exists<T>(T[] array, Predicate<T> match)
Determines whether the specified array contains any element that matches the conditions defined by the specified predicate.
- array
- The array to search.
- match
- The predicate that defines the conditions of the elements to search for.
true
, if the array contains one or more elements that match the conditions defined by the specified predicate; otherwise,false
.
Exception Type Condition ArgumentNullException array or match is null
.
The predicate returnstrue
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 returnstrue
.
System.Array Class, System Namespace
public static T Find<T>(T[] array, Predicate<T> match)
Searches for an element that matches the predicate, and returns the first occurrence within the entire array.
- array
- The array to search.
- match
- The predicate that defines the conditions of the element to search for.
The first element that matches the conditions defined by the specified predicate, if found; otherwise, the default value for type T.
Exception Type Condition ArgumentNullException array or match is null
.
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 returnstrue
.
System.Array Class, System Namespace
public static T[] FindAll<T>(T[] array, Predicate<T> match)
Retrieves all the elements that match the conditions defined by the specified predicate.
- array
- The array to search.
- match
- The predicate that specifies the elements to search for.
An array containing all the elements that match the conditions defined by the specified predicate, if found; otherwise, an empty array.
Exception Type Condition ArgumentNullException array or match is null
.
The elements of array are individually passed to the predicate, and those elements for which the predicate returnstrue
, are saved in the returned array.
System.Array Class, System Namespace
public static int FindIndex<T>(T[] array, Predicate<T> match)
Searches for an element that matches the predicate, and returns the zero-based index of the first occurrence within the entire array.
- array
- The array to search.
- match
- The predicate that specifies the elements to search for.
The zero-based index of the first occurrence of an element that matches the conditions defined by match, if found; otherwise, -1.
Exception Type Condition ArgumentNullException array or match is null
.
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 returnstrue
.
System.Array Class, System Namespace
public static int FindIndex<T>(T[] array, int startIndex, Predicate<T> match)
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.
- array
- The array to search.
- startIndex
- The zero-based starting index of the search.
- match
- The predicate that specifies the elements to search for.
The zero-based index of the first occurrence of an element that matches the conditions defined by match, if found; otherwise, -1.
Exception Type Condition ArgumentNullException array or match is null
.ArgumentOutOfRangeException startIndex is less than zero or greater than array .Length
.
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 returnstrue
.
System.Array Class, System Namespace
public static int FindIndex<T>(T[] array, int startIndex, int count, Predicate<T> match)
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.
- 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.
The zero-based index of the first occurrence of an element that matches the conditions defined by match, if found; otherwise, -1.
Exception Type Condition ArgumentNullException array or match is null
.ArgumentOutOfRangeException startIndex is less than zero. -or-
count is less than zero.
-or-
startIndex + count is greater than array
.Length
.
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 returnstrue
.
System.Array Class, System Namespace
public static T FindLast<T>(T[] array, Predicate<T> match)
Searches for an element that matches the predicate, and returns the last occurrence within the entire array.
- array
- The array to search.
- match
- The predicate that specifies the elements to search for.
The last element that matches the conditions defined by the specified predicate, if found; otherwise, the default value for type T.
Exception Type Condition ArgumentNullException array or match is null
.
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.
System.Array Class, System Namespace
public static int FindLastIndex<T>(T[] array, Predicate<T> match)
Searches for an element that matches the predicate, and returns the zero-based index of the last occurrence within the entire array.
- array
- The array to search.
- match
- The predicate that specifies the elements to search for.
The zero-based index of the first occurrence of an element that matches the conditions defined by match, if found; otherwise, -1.
Exception Type Condition ArgumentNullException array or match is null
.
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 returnstrue
.
System.Array Class, System Namespace
public static int FindLastIndex<T>(T[] array, int startIndex, Predicate<T> match)
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.
- 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.
The zero-based index of the first occurrence of an element that matches the conditions defined by match, if found; otherwise, -1.
Exception Type Condition ArgumentNullException array or match is null
.ArgumentOutOfRangeException startIndex is less than zero or greater than array .Length
.
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 returnstrue
.
System.Array Class, System Namespace
public static int FindLastIndex<T>(T[] array, int startIndex, int count, Predicate<T> match)
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.
- 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.
The zero-based index of the first occurrence of an element that matches the conditions defined by match, if found; otherwise, -1.
Exception Type Condition ArgumentNullException array or match is null
.ArgumentOutOfRangeException startIndex is less than zero or greater than array .Length
.-or-
count is less than zero.
-or-
count is greater than startIndex + 1.
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 returnstrue
.
System.Array Class, System Namespace
public static void ForEach<T>(T[] array, Action<T> action)
Performs the specified action on each element of the specified array.
- array
- The array on whose elements the action is to be performed.
- action
- The action to perform on each element of array.
The first element that matches the conditions defined by the specified predicate, if found; otherwise, the default value for type T.
Exception Type Condition ArgumentNullException array or action is null
.
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 callForEach
. Execution stops if the action throws an exception.
System.Array Class, System Namespace
public virtual IEnumerator GetEnumerator();
Returns a IEnumerator for the current instance.
A IEnumerator for the current instance.
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 returnedfalse
, 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.]
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
System.Array Class, System Namespace
public int GetLength(int dimension)
Gets the number of elements in the specified dimension of the array.
- dimension
- The zero-based dimension of the array whose length is to be determined.
The number of elements in the specified dimension of the array.
Exception Type Condition IndexOutOfRangeException dimension is less than zero. -or-
dimension is equal to or greater than System.Array.Rank.
RuntimeInfrastructure
System.Array Class, System Namespace
public int GetLowerBound(int dimension);
Returns the lower bound of the specified dimension in the current instance.
- dimension
- A Int32 that contains the zero-based dimension of the current instance whose lower bound is to be determined.
A Int32 that contains the lower bound of the specified dimension in the current instance.
Exception Type Condition IndexOutOfRangeException dimension < 0. -or-
dimension is equal to or greater than the System.Array.Rank property of the current instance.
[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.]
RuntimeInfrastructure
System.Array Class, System Namespace
public int GetUpperBound(int dimension);
Returns the upper bound of the specified dimension in the current instance.
- dimension
- A Int32 that contains the zero-based dimension of the current instance whose upper bound is to be determined.
A Int32 that contains the upper bound of the specified dimension in the current instance.
Exception Type Condition IndexOutOfRangeException dimension < 0. -or-
dimension is equal to or greater than the System.Array.Rank property of the current instance.
[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.]
RuntimeInfrastructure
System.Array Class, System Namespace
public object GetValue(int[] indices);
Gets the value at the specified position in the current multidimensional instance.
- 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.
A Object that contains the value at the specified position in the current instance.
Exception Type Condition ArgumentNullException indices is null
.ArgumentException The number of dimensions in the current instance is not equal to the number of elements in indices. IndexOutOfRangeException At least one element in indices is outside the range of valid indices for the corresponding dimension of the current instance.
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.]
ExtendedArray
System.Array Class, System Namespace
public object GetValue(int index);
Gets the value at the specified position in the current one-dimensional instance.
- index
- A Int32 that contains the position of the value to get from the current instance.
A Object that contains the value at the specified position in the current instance.
Exception Type Condition ArgumentException The current instance has more than one dimension. IndexOutOfRangeException index is outside the range of valid indices for the current instance.
[Note: Use the System.Array.GetLowerBound(System.Int32) and System.Array.GetUpperBound(System.Int32) methods to determine whether index is out of bounds.]
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'
System.Array Class, System Namespace
public object GetValue(int index1, int index2);
Gets the value at the specified position in the current two-dimensional instance.
- 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.
A Object that contains the value at the specified position in the current instance.
Exception Type Condition ArgumentException The current instance does not have exactly two dimensions. IndexOutOfRangeException At least one of index1 or index2 is outside the range of valid indexes for the corresponding dimension of the current instance.
[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.]
ExtendedArray
System.Array Class, System Namespace
public object GetValue(int index1, int index2, int index3);
Gets the value at the specified position in the current three-dimensional instance.
- 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.
A Object that contains the value at the specified position in the current instance.
Exception Type Condition ArgumentException The current instance does not have exactly three dimensions. IndexOutOfRangeException At least one ofindex1 or index2 or index3 is outside the range of valid indexes for the corresponding dimension of the current instance.
[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.]
ExtendedArray
System.Array Class, System Namespace
public static int IndexOf(Array array, object value, int startIndex, int count);
Searches the specified one-dimensional Array, returning the index of the first occurrence of the specified Object in the specified range.
- 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.
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.]
Exception Type Condition ArgumentNullException array is null
.
ArgumentOutOfRangeException startIndex is less than array .GetLowerBound(0)
.-or-
count is less than zero.
-or-
startIndex + count is greater than array
.GetLowerBound(0)
+ array.Length
.
RankException array has more than one dimension.
The elements are compared using System.Object.Equals(System.Object).
System.Array Class, System Namespace
public static int IndexOf(Array array, object value, int startIndex);
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.
- 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.
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.]
Exception Type Condition ArgumentNullException array is null
.
ArgumentOutOfRangeException startIndex is less than array .GetLowerBound(0)
or greater than array.GetLowerBound(0)
+ array.Length
.
RankException array has more than one dimension.
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).
System.Array Class, System Namespace
public static int IndexOf(Array array, object value);
Searches the specified one-dimensional Array, returning the index of the first occurrence of the specified Object.
- array
- A one-dimensional Array to search.
- value
- A Object to locate in array.
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.]
Exception Type Condition ArgumentNullException array is null
.RankException array has more than one dimension.
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).
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
System.Array Class, System Namespace
public static int IndexOf<T>(T[] array, T value, int startIndex, int count)
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.
- 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.
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.
Exception Type Condition ArgumentNullException array is null
.
ArgumentOutOfRangeException startIndex is less than zero. -or-
count is less than zero.
-or-
startIndex + count is greater than System.Array.Length.
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 returnstrue
.
System.Array Class, System Namespace
public static int IndexOf<T>(T[] array, T value, int startIndex)
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.
- array
- The array to search.
- value
- The value to locate.
- startIndex
- The zero-based starting index of the search.
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.
Exception Type Condition ArgumentNullException array is null
.
ArgumentOutOfRangeException startIndex is less than zero or greater than array .Length
.
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 returnstrue
.
System.Array Class, System Namespace
public static int IndexOf<T>(T[] array, T value)
Searches the specified array, returning the index of the first occurrence of the specified value.
- array
- The array to search.
- value
- The value to locate.
The zero-based index of the first occurrence of value in array, if found; otherwise, - 1.
Exception Type Condition ArgumentNullException array is null
.
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 returnstrue
.
System.Array Class, System Namespace
public void Initialize();
Initializes every element of the current instance of value-type objects by calling the default constructor of that value type.
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. ]
RuntimeInfrastructure
System.Array Class, System Namespace
public static int LastIndexOf(Array array, object value, int startIndex, int count);
Searches the specified one-dimensional Array, returning the index of the last occurrence of the specified Object in the specified range.
- 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 .
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.]
Exception Type Condition ArgumentNullException array is null
.
ArgumentOutOfRangeException startIndex is outside the range of valid indices for array. -or-
count < 0.
-or-
count is greater than startIndex + 1.
RankException array has more than one dimension.
The elements are compared using System.Object.Equals(System.Object).
System.Array Class, System Namespace
public static int LastIndexOf(Array array, object value, int startIndex);
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.
- 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.
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.]
Exception Type Condition ArgumentNullException array is null
.
ArgumentOutOfRangeException startIndex is outside the range of valid indices for array.
RankException array has more than one dimension.
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).
System.Array Class, System Namespace
public static int LastIndexOf(Array array, object value);
Searches the specified one-dimensional Array, returning the index of the last occurrence of the specified Object.
- array
- A one-dimensional Array to search.
- value
- A Object to locate in array.
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.]
Exception Type Condition ArgumentNullException array is null
.RankException array has more than one dimension.
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).
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
System.Array Class, System Namespace
public static int LastIndexOf<T>(T[] array, T value, int startIndex, int count)
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.
- 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.
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.
Exception Type Condition ArgumentNullException array is null
.
ArgumentOutOfRangeException startIndex is outside the range of valid indices for array. -or-
count is less than zero.
-or-
count is greater than startIndex + 1.
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 returnstrue
.
System.Array Class, System Namespace
public static int LastIndexOf<T>(T[] array, T value, int startIndex)
Searches the specified array backwards, returning the index of the last occurrence of the specified array, starting at the specified index.
- array
- The array to search.
- value
- The value to locate.
- startIndex
- The zero-based starting index of the search.
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.
Exception Type Condition ArgumentNullException array is null
.
ArgumentOutOfRangeException startIndex is outside the range of valid indices for array.
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 returnstrue
.
System.Array Class, System Namespace
public static int LastIndexOf<T>(T[] array, T value)
Searches the specified array, returning the index of the last occurrence of the specified value.
- array
- The array to search.
- value
- The value to locate.
The zero-based index of the last occurrence of value in array, if found; otherwise, - 1.
Exception Type Condition ArgumentNullException array is null
.
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 returnstrue
.
System.Array Class, System Namespace
public static void Resize<T>(ref T[] array, int newSize)
Changes the size of an array to the specified new size.
- array
- The array to resize.
-or-
null
to create a new array with the specified size.
- newSize
- The size of the new array.
Exception Type Condition ArgumentOutOfRangeException newSize is less than zero.
If array isnull
, 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.
System.Array Class, System Namespace
public static void Reverse(Array array, int index, int length);
Reverses the sequence of the elements in the specified range of the specified one-dimensional Array.
- 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.
Exception Type Condition ArgumentNullException array is null
.RankException array is multidimensional. ArgumentOutOfRangeException index < array.GetLowerBound(0). length < 0.
ArgumentException index and length do not specify a valid range in array (i.e. index + length > array.GetLowerBound(0) + array.Length).
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
System.Array Class, System Namespace
public static void Reverse(Array array);
Reverses the sequence of the elements in the specified one-dimensional Array.
- array
- The one-dimensional Array to reverse.
Exception Type Condition ArgumentNullException array is null
.RankException array has more than one dimension.
This version of System.Array.Reverse(System.Array) is equivalent to System.Array.Reverse(System.Array)(array, array.GetLowerBound(0), array.Length).
System.Array Class, System Namespace
public void SetValue(object value, int index);
Sets the value of the element at the specified position in the current one-dimensional instance.
- 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.
Exception Type Condition ArgumentException The current instance has more than one dimension.
IndexOutOfRangeException index is outside the range of valid indices for the current instance.
InvalidCastException value is not assignment-compatible with the element type of the current instance.
[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.
]
System.Array Class, System Namespace
public void SetValue(object value, int index1, int index2);
Sets the value of the element at the specified position in the current two-dimensional instance.
- 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.
Exception Type Condition ArgumentException The current instance does not have exactly two dimensions.
IndexOutOfRangeException At least one of index1 or index2 is outside the range of valid indices for the corresponding dimension of the current instance. InvalidCastException value is not assignment-compatible with the element type of 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 indices are out of bounds.
]
ExtendedArray
System.Array Class, System Namespace
public void SetValue(object value, int index1, int index2, int index3);
Sets the value of the element at the specified position in the current three-dimensional instance.
- 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.
Exception Type Condition ArgumentException The current instance does not have exactly three dimensions.
IndexOutOfRangeException At least one of index1, index2, or index3 is outside the range of valid indices for the corresponding dimension of the current instance. InvalidCastException value is not assignment-compatible with the element type of 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 indices are out of bounds.
]
ExtendedArray
System.Array Class, System Namespace
public void SetValue(object value, int[] indices);
Sets the value of the element at the specified position in the current multidimensional instance.
- 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.
Exception Type Condition ArgumentNullException indices is null
.ArgumentException The number of dimensions in the current instance is not equal to the number of elements in indices.
IndexOutOfRangeException At least one element in indices is outside the range of valid indices for the corresponding dimension of the current instance.
InvalidCastException value is not assignment-compatible with the element type of the current instance.
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.
]
ExtendedArray
System.Array Class, System Namespace
public static void Sort(Array keys, Array items, int index, int length, IComparer comparer);
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.
- 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.
Exception Type Condition ArgumentNullException keys is null
.
RankException keys has more than one dimension. -or-
items is not a null reference and has more than one dimension.
ArgumentOutOfRangeException index < keys.GetLowerBound(0). -or-
length < 0.
ArgumentException items 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.
InvalidOperationException comparer is null
, and one or more elements in keys that are used in a comparison do not implement the IComparable interface.
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.
System.Array Class, System Namespace
public static void Sort(Array array, int index, int length, IComparer comparer);
Sorts the elements in the specified section of the specified one-dimensional Array using the specified IComparer implementation.
- 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.
Exception Type Condition ArgumentNullException array is null
.RankException array has more than one dimension. ArgumentOutOfRangeException index < array.GetLowerBound(0). -or-
length < 0.
ArgumentException index and length do not specify a valid range in array.
InvalidOperationException comparer is null
, and one or more elements in array that are used in a comparison do not implement the IComparable interface.
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.
System.Array Class, System Namespace
public static void Sort(Array keys, Array items, IComparer comparer);
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.
- 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.
Exception Type Condition ArgumentNullException keys is null
.
RankException keys has more than one dimension. -or-
items is not a null reference and has more than one dimension.
ArgumentException items 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.
InvalidOperationException comparer is a null
, and one or more elements in keys that are used in a comparison do not implement the IComparable interface.
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.
System.Array Class, System Namespace
public static void Sort(Array array, IComparer comparer);
Sorts the elements in the specified one-dimensional Array using the specified IComparer implementation.
- 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.
Exception Type Condition ArgumentNullException array is null
.RankException array has more than one dimension. InvalidOperationException comparer is a null reference, and one or more elements in array that are used in a comparison do not implement the IComparable interface.
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.
System.Array Class, System Namespace
public static void Sort(Array keys, Array items, int index, int length);
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.
- 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.
Exception Type Condition ArgumentNullException keys is null
.
RankException keys has more than one dimension. -or-
items is not a null reference and has more than one dimension.
ArgumentOutOfRangeException index < keys.GetLowerBound(0). -or-
length < 0.
ArgumentException items 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.
InvalidOperationException One or more elements in keys that are used in a comparison do not implement the IComparable interface.
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.
System.Array Class, System Namespace
public static void Sort(Array array);
Sorts the elements of the specified one-dimensional Array.
- array
- A one-dimensional Array to sort.
Exception Type Condition ArgumentNullException array is null
.RankException array has more than one dimension. InvalidOperationException One or more elements in array that are used in a comparison do not implement the IComparable interface.
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.
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
System.Array Class, System Namespace
public static void Sort(Array keys, Array items);
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.
- 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.
Exception Type Condition ArgumentNullException keys is null
.
RankException keys has more than one dimension. -or-
items is not a null reference and has more than one dimension.
ArgumentException items 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.
InvalidOperationException One or more elements in keys that are used in a comparison do not implement the IComparable interface.
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.
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
System.Array Class, System Namespace
public static void Sort(Array array, int index, int length);
Sorts the elements in the specified range of the specified one-dimensional Array.
- 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.
Exception Type Condition ArgumentNullException array is null
.RankException array has more than one dimension. ArgumentOutOfRangeException index < array.GetLowerBound(0). -or-
length < 0.
ArgumentException index and length do not specify a valid range in array.
InvalidOperationException One or more elements in array that are used in a comparison do not implement the IComparable interface.
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.
System.Array Class, System Namespace
public static void Sort<K,V>(K[] keys, V[] items, int index, int length, IComparer<K> comparer)
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>.
- 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.
Exception Type Condition ArgumentException index 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.
ArgumentNullException keys is null
.
ArgumentOutOfRangeException index is less than zero. -or-
length is less than zero.
InvalidOperationException comparer 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.
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.
System.Array Class, System Namespace
public static void Sort<K,V>(K[] keys, V[] items, IComparer<K> comparer)
Sorts a pair of arrays based on the keys in the first array, using the specified System.Collections.Generic.IComparer.
- 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.
Exception Type Condition ArgumentNullException keys is null
.
ArgumentException items is not null
, and the length of keys does not match the length of items.
InvalidOperationException comparer 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.
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.
System.Array Class, System Namespace
public static void Sort<K,V>(K[] keys, V[] items, int index, int length)
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.
- 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.
Exception Type Condition ArgumentException index 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.
ArgumentNullException keys is null
.
ArgumentOutOfRangeException index is less than zero. -or-
length is less than zero.
InvalidOperationException One 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.
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.
System.Array Class, System Namespace
public static void Sort<K,V>(K[] keys, V[] items)
Sorts a pair of arrays based on the keys in the first array using the IComparable implementation of each key.
- 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.
Exception Type Condition ArgumentException items is not null
, and the length of keys does not equal the length of items.
ArgumentNullException keys is null
.
InvalidOperationException One 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.
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.
System.Array Class, System Namespace
public static void Sort<T>(T[] array, int index, int length, IComparer<T> comparer)
Sorts the elements in a range of elements in an array using the specified comparer.
- 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.
Exception Type Condition ArgumentNullException array is null
.
ArgumentOutOfRangeException index is less than zero. -or-
length is less than zero.
ArgumentException index and length do not specify a valid range in array.
InvalidOperationException comparer 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.
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.
System.Array Class, System Namespace
public static void Sort<T>(T[] array, IComparer<T> comparer)
Sorts the elements in an array using the specified comparer.
- 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.
Exception Type Condition ArgumentNullException array is null
.
InvalidOperationException comparer 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.
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.
System.Array Class, System Namespace
public static void Sort<T>(T[] array, Comparison<T> comparison)
Sorts the elements in an array using the specified comparison.
- array
- The array to sort.
- comparison
- The Comparison<T> to use when comparing elements.
Exception Type Condition ArgumentNullException array is null
.-or-
comparison is
null
.
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.
System.Array Class, System Namespace
public static void Sort<T>(T[] array)
Sorts the elements in an entire array using the IComparable<T> or IComparable implementation of each element of that array.
- array
- The array to sort.
Exception Type Condition ArgumentNullException array is null
.
InvalidOperationException One 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.
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.
System.Array Class, System Namespace
public static void Sort<T>(T[] array, int index, int length)
Sorts an array using the IComparable<T> or IComparable implementation of each element of that array.
- array
- The array to sort.
- index
- The starting index of the range to sort.
- length
- The number of elements in the range to sort.
Exception Type Condition ArgumentException index and length do not specify a valid range in array.
ArgumentNullException array is null
.
ArgumentOutOfRangeException index is less than zero. -or-
length is less than zero.
InvalidOperationException One or more elements in array that are used in a comparison do not implement the IComparable<T> or System.IComparable interface.
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.
System.Array Class, System Namespace
int IList.Add(object value);
Implemented to support the IList interface. [Note: For more information, see System.Collections.IList.Add.]
System.Array Class, System Namespace
void IList.Clear();
Implemented to support the IList interface. [Note: For more information, see System.Collections.IList.Clear.]
System.Array Class, System Namespace
bool IList.Contains(object value);
Implemented to support the IList interface. [Note: For more information, see System.Collections.IList.Contains.]
System.Array Class, System Namespace
int IList.IndexOf(object value);
Implemented to support the IList interface. [Note: For more information, see System.Collections.IList.IndexOf.]
System.Array Class, System Namespace
void IList.Insert(int index, object value);
Implemented to support the IList interface. [Note: For more information, see System.Collections.IList.Insert.]
System.Array Class, System Namespace
void IList.Remove(object value);
Implemented to support the IList interface. [Note: For more information, see System.Collections.IList.Remove.]
System.Array Class, System Namespace
void IList.RemoveAt(int index);
Implemented to support the IList interface. [Note: For more information, see System.Collections.IList.RemoveAt.]
System.Array Class, System Namespace
public static bool TrueForAll<T>(T[] array, Predicate<T> match)
Determines whether every element in the array matches the predicate.
- array
- The array to check against the conditions.
- match
- The predicate against which the elements are checked..
true
, if every element in array matches the specified predicate; otherwise,false
.
Exception Type Condition ArgumentNullException array or match is null
.
The predicate returnstrue
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 returnsfalse
for any element.
System.Array Class, System Namespace
public bool IsFixedSize { get; }
Implemented to support the IList interface. [Note: For more information, see System.Collections.IList.IsFixedSize.]
System.Array Class, System Namespace
public bool IsReadOnly { get; }
Implemented to support the IList interface. [Note: For more information, see System.Collections.IList.IsReadOnly.]
System.Array Class, System Namespace
public bool IsSynchronized { get; }
Implemented to support the ICollection interface. [Note: For more information, see System.Collections.ICollection.IsSynchronized.]
System.Array Class, System Namespace
public int Length { get; }
Gets the total number of elements in all the dimensions of the current instance.
A Int32 that contains the total number of elements in all the dimensions of the current instance.
This property is read-only.
System.Array Class, System Namespace
public long LongLength {get;}
Gets the total number of elements in all the dimensions of the current instance.
A Int64 value containing the length of the array.
This property is read-only.
System.Array Class, System Namespace
public int Rank { get; }
Gets the rank (number of dimensions) of the current instance.
A Int32 that contains the rank (number of dimensions) of the current instance.
This property is read-only.
System.Array Class, System Namespace
public object SyncRoot { get; }
Implemented to support the ICollection interface. [Note: For more information, see System.Collections.ICollection.SyncRoot.]
System.Array Class, System Namespace
int ICollection.Count { get; }
Implemented to support the ICollection interface. [Note: For more information, see System.Collections.ICollection.Count.]
System.Array Class, System Namespace
public virtual object this[int index] { get; set; }
Implemented to support the IList interface. [Note: For more information, see System.Collections.IList.Item.]
System.Array Class, System Namespace