public abstract class Array : ICloneable, ICollection, IEnumerable, IList
Object
ArrayThis type implements ICloneable, IList, ICollection, and IEnumerable.
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 interface. 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 positive 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.
System Namespace
Array Constructors
Array Methods
Array.BinarySearch(System.Array, System.Object) Method
Array.BinarySearch(System.Array, int, int, System.Object) Method
Array.BinarySearch(System.Array, System.Object, System.Collections.IComparer) Method
Array.BinarySearch(System.Array, int, int, System.Object, System.Collections.IComparer) Method
Array.Clear Method
Array.Clone 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) Method
Array.CreateInstance(System.Type, int, int, int) Method
Array.CreateInstance(System.Type, int[]) Method
Array.CreateInstance(System.Type, int[], int[]) Method
Array.GetEnumerator 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) Method
Array.IndexOf(System.Array, System.Object, int) Method
Array.IndexOf(System.Array, System.Object, int, int) Method
Array.Initialize Method
Array.LastIndexOf(System.Array, System.Object) Method
Array.LastIndexOf(System.Array, System.Object, int) Method
Array.LastIndexOf(System.Array, System.Object, int, int) Method
Array.Reverse(System.Array) Method
Array.Reverse(System.Array, int, int) 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) Method
Array.Sort(System.Array, System.Array) Method
Array.Sort(System.Array, int, int) Method
Array.Sort(System.Array, System.Array, int, int) Method
Array.Sort(System.Array, System.Collections.IComparer) Method
Array.Sort(System.Array, System.Array, System.Collections.IComparer) Method
Array.Sort(System.Array, int, int, System.Collections.IComparer) Method
Array.Sort(System.Array, System.Array, int, int, System.Collections.IComparer) 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 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
protected Array();
Constructs a new instance of the Array class.
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, or a null reference. [Note: A null reference will be considered to compare less than any non-null object, or equal to another null reference.]
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 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 ArgumentException Both value and at least one element of array do not implement the IComparable interface. -or-
value is not assignment-compatible with at least one element of array.
-or-
array.UpperBound == System.Int32.MaxValue.
ArgumentNullException array is null
.RankException array has more than one dimension.
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 ArgumentException 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, or a null reference. [Note: A null reference will be considered to compare less than any non-null object, or equal to another null reference.]
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 if array is already sorted.]
Return Value Description The index of value in the array. value was found. The bitwise complement 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 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.
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-
Either value or at least one element of array does not implement the IComparable interface.
-or-
value is not assignment-compatible with at least one element of array.
-or-
array.UpperBound == System.Int32.MaxValue.
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 ArgumentException 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, 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, or a null reference. [Note: A null reference will be considered to compare less than any non-null object, or equal to another null reference.]
- 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 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 ArgumentException comparer is null
, and both value and at least one element of array do not implement the IComparable interface.-or-
comparer is
null
, and value is not assignment-compatible with at least one element of array.-or-
array.UpperBound == System.Int32.MaxValue.
ArgumentNullException array is null
.RankException array has more than one dimension.
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 ArgumentException 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, 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, or a null reference. [Note: A null reference will be considered to compare less than any non-null object, or equal to another null reference.]
- 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 if array is already sorted.]
Return Value Description The index of value in the array. value was found. The bitwise complement 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 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.
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-
comparer is
null
, and both value and at least one element of array do not implement the IComparable interface.-or-
comparer is
null
, and value is not of the same type as the elements of array.-or-
array.UpperBound == System.Int32.MaxValue.
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 ArgumentException 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 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 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 instance to the specified one-dimensional array starting at the specified relative index 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 relative zero-based 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 < 0. ArgumentException array has more than one dimension. -or-
index is greater than or equal to 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 type of the current instance cannot be cast automatically to the type of array.
[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.index is a relative index, not the actual array index. If the index of array is zero-based, this value is the same as the actual index at which copying begins. If the lower bound of array is not zero, the value of index is added to the lower bound of array to get the actual index at which copying begins. For example, if the lower bound of array is 2 and the value of index is 1, the copying actually starts at index 3.
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 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 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 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[] 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 orlengths 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[] lengths, int[] lowerBounds);
Creates a multidimensional array of the specified Type and dimension lengths, with the specified lower bounds.
- 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 of the specified Type 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 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: 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.
The enumerator is in an invalid state if it is positioned before the first element or after the last element of the current instance. Whenever the enumerator is in an invalid state, a call to System.Collections.IEnumerator.Current is required to throw a InvalidOperationException.
System.Collections.IEnumerator.Current returns the same object until either System.Collections.IEnumerator.MoveNext or System.Collections.IEnumerator.Reset is called.
Once the enumerator of the current instance is moved immediately past the last element of the current instance, subsequent calls to System.Collections.IEnumerator.MoveNext return
false
and the enumerator remains positioned immediately past the last element.]
[Default: Multidimensional arrays will be processed in Row-major form.
[Note: For some multidimensional Array objects, it may 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 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);
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 - array.GetLowerBound(0)) ).The elements will be compared using the semantics of the System.Object.Equals(System.Object) method. For user-defined types, System.Object.Equals(System.Object) is actually called.
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(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 outside the range of valid indexes for array.
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)).The elements will be compared using the semantics of the System.Object.Equals(System.Object) method. For user-defined types, System.Object.Equals(System.Object) is actually called.
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, 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-
The sum of count and startIndex does not specify a valid range in array (i.e. count + startIndex > array.GetLowerBound(0) + array.Length).
RankException array has more than one dimension.
The elements will be compared using the semantics of the System.Object.Equals(System.Object) method. For user-defined types, System.Object.Equals(System.Object) is actually called.
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);
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), array.Length).The elements will be compared using the semantics of the System.Object.Equals(System.Object) method. For user-defined types, System.Object.Equals(System.Object) is actually called.
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(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, (array.Length - startIndex) ).The elements will be compared using the semantics of the System.Object.Equals(System.Object) method. For user-defined types, System.Object.Equals(System.Object) is actually called.
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 + count through startIndex, 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 and startIndex do not specify a valid range in array.
RankException array has more than one dimension.
The elements will be compared using the semantics of the System.Object.Equals(System.Object) method. For user-defined types, System.Object.Equals(System.Object) is actually called.
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 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 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. -or-
value is not assignment-compatible with the element type of the current instance.
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.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. -or-
value is not assignment-compatible with the element type of the current instance.
IndexOutOfRangeException At least one of index1 or index2 is outside the range of valid indices for the corresponding dimension 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. -or-
value is not assignment-compatible with the element type of the current instance.
IndexOutOfRangeException At least one of index1, index2, or index3 is outside the range of valid indices for the corresponding dimension 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. -or-
value is not assignment-compatible with the element type of the current instance.
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: 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 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. ArgumentException One or more elements in array 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.
-or-
One or more elements in keys 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. -or-
One or more elements in array 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(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 key.
-or-
items is not a null reference, and index and length do not specify a valid range in items.
-or-
One or more elements in keys 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, 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. ArgumentException comparer is a null reference, and one or more elements in array 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, 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.
-or-
comparer is a null reference, and one or more elements in the keys 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, 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. -or-
comparer is a null reference, and one or more elements in array 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, 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.
-or-
comparer is a null reference, and one or more elements in keys 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
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
bool IList.IsFixedSize { get; }
Implemented to support the IList interface. [Note: For more information, see System.Collections.IList.IsFixedSize.]
System.Array Class, System Namespace
bool IList.IsReadOnly { get; }
Implemented to support the IList interface. [Note: For more information, see System.Collections.IList.IsReadOnly.]
System.Array Class, System Namespace
bool ICollection.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.[Note: For additional information, see System.Array.Length.]
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
object ICollection.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