Table of Contents

Class Array

Namespace
System
Assembly
mscorlib.dll

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

public abstract class Array : ICloneable, IList, ICollection, IEnumerable
Inheritance
Array
Implements
Inherited Members
Extension Methods

Constructors

Array()

protected Array()

Properties

IsFixedSize

Gets a value indicating whether the Array has a fixed size.

public bool IsFixedSize { get; }

Property Value

bool

This property is always true for all arrays.

IsReadOnly

Gets a value indicating whether the Array is read-only.

public bool IsReadOnly { get; }

Property Value

bool

This property is always false for all arrays.

IsSynchronized

Gets a value indicating whether access to the Array is synchronized (thread safe).

public bool IsSynchronized { get; }

Property Value

bool

This property is always false for all arrays.

Length

Gets a 32-bit integer that represents the total number of elements in all the dimensions of the Array.

public int Length { get; }

Property Value

int

A 32-bit integer that represents the total number of elements in all the dimensions of the Array; zero if there are no elements in the array.

SyncRoot

Gets an object that can be used to synchronize access to the Array.

public object SyncRoot { get; }

Property Value

object

An object that can be used to synchronize access to the Array.

Methods

BinarySearch(Array, int, int, object, IComparer)

Searches a range of elements in a one-dimensional sorted Array for a value, using the specified IComparer interface.

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

Parameters

array Array

The sorted one-dimensional Array to search.

index int

The starting index of the range to search.

length int

The length of the range to search.

value object

The object to search for.

comparer IComparer

The IComparer implementation to use when comparing elements.

-or-

nullNothingnullptrunit a null reference(Nothing in Visual Basic) to use the IComparable implementation of each element.

Returns

int

The index of the specified value in the specified array, if value is found. If value is not found and value is less than one or more elements in array, a negative number which is the bitwise complement of the index of the first element that is larger than value. If value is not found and value is greater than any of the elements in array, a negative number which is the bitwise complement of (the index of the last element plus 1).

Exceptions

InvalidOperationException

comparer is null reference (Nothing in Visual Basic), value does not implement the IComparable interface, and the search encounters an element that does not implement the IComparable interface.

BinarySearch(Array, object, IComparer)

Searches an entire one-dimensional sorted Array for a value using the specified IComparer interface.

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

Parameters

array Array

The sorted one-dimensional Array to search.

value object

The object to search for.

comparer IComparer

The IComparer implementation to use when comparing elements.

-or-

nullNothingnullptrunit a null reference(Nothing in Visual Basic) to use the IComparable implementation of each element.

Returns

int

The index of the specified value in the specified array, if value is found. If value is not found and value is less than one or more elements in array, a negative number which is the bitwise complement of the index of the first element that is larger than value. If value is not found and value is greater than any of the elements in array, a negative number which is the bitwise complement of (the index of the last element plus 1).

Clear(Array, int, int)

Sets a range of elements in the Array to zero, to false, or to null reference (Nothing in Visual Basic), depending on the element type.

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

Parameters

array Array

The Array whose elements need to be cleared.

index int

The starting index of the range of elements to clear.

length int

The number of elements to clear.

Clone()

Creates a shallow copy of the Array.

public object Clone()

Returns

object

A shallow copy of the Array.

Remarks

Available only in mscorlib build with support for System.Reflection.

Copy(Array, Array, int)

Copies a range of elements from an Array starting at the first element and pastes them into another Array starting at the first element. The length is specified as a 32-bit integer.

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

Parameters

sourceArray Array

The Array that contains the data to copy.

destinationArray Array

The Array that receives the data.

length int

A 32-bit integer that represents the number of elements to copy.

Copy(Array, int, Array, int, int)

Copies a range of elements from an Array starting at the specified source index and pastes them to another Array starting at the specified destination index. The length and the indexes are specified as 32-bit integers.

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

Parameters

sourceArray Array

The Array that contains the data to copy.

sourceIndex int

A 32-bit integer that represents the index in the sourceArray at which copying begins.

destinationArray Array

The Array that receives the data.

destinationIndex int

A 32-bit integer that represents the index in the destinationArray at which storing begins.

length int

A 32-bit integer that represents the number of elements to copy.

CopyTo(Array, int)

Copies all the elements of the current one-dimensional Array to the specified one-dimensional Array starting at the specified destination Array index. The index is specified as a 32-bit integer.

public void CopyTo(Array array, int index)

Parameters

array Array

The one-dimensional Array that is the destination of the elements copied from the current Array.

index int

A 32-bit integer that represents the index in array at which copying begins.

CreateInstance(Type, int)

Creates a one-dimensional Array of the specified Type and length, with zero-based indexing.

public static Array CreateInstance(Type elementType, int length)

Parameters

elementType Type

The Type of the Array to create.

length int

The size of the Array to create.

Returns

Array

A new one-dimensional Array of the specified Type with the specified length, using zero-based indexing.

Remarks

Unlike most classes, Array provides the CreateInstance method, instead of public constructors, to allow for late bound access.

Reference-type elements are initialized to nullNothingnullptrunit a null reference(Nothing in Visual Basic). Value-type elements are initialized to zero.

This method is an O(n) operation, where n is length.

Available only in mscorlib build with support for System.Reflection.

GetEnumerator()

Returns an IEnumerator for the Array.

public IEnumerator GetEnumerator()

Returns

IEnumerator

An IEnumerator for the Array.

GetValue(int)

Gets the value at the specified position in the one-dimensional Array. The index is specified as a 32-bit integer.

public object GetValue(int index)

Parameters

index int

A 32-bit integer that represents the position of the Array element to get.

Returns

object

The value at the specified position in the one-dimensional Array.

IndexOf(Array, object)

Searches for the specified object and returns the index of the first occurrence within the entire one-dimensional Array.

public static int IndexOf(Array array, object value)

Parameters

array Array

The one-dimensional Array to search.

value object

The object to locate in array.

Returns

int

The index of the first occurrence of value within the entire array, if found; otherwise, the lower bound of the array minus 1.

IndexOf(Array, object, int)

Searches for the specified object and returns the index of the first occurrence within the range of elements in the one-dimensional Array that extends from the specified index to the last element.

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

Parameters

array Array

The one-dimensional Array to search.

value object

The object to locate in array.

startIndex int

The starting index of the search. 0 (zero) is valid in an empty array.

Returns

int

The 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, the lower bound of the array minus 1.

IndexOf(Array, object, int, int)

Searches for the specified object and returns the index of the first occurrence within the range of elements in the one-dimensional Array that starts at the specified index and contains the specified number of elements.

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

Parameters

array Array

The one-dimensional Array to search.

value object

The object to locate in array.

startIndex int

The starting index of the search. 0 (zero) is valid in an empty array.

count int

The number of elements in the section to search.

Returns

int

The 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, the lower bound of the array minus 1.