Share via


IndexOf Method (Array, Object, Int32)

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

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.

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

Syntax

'Declaration
Public Shared Function IndexOf ( _
    array As Array, _
    value As Object, _
    startIndex As Integer _
) As Integer
public static int IndexOf(
    Array array,
    Object value,
    int startIndex
)
public:
static int IndexOf(
    Array^ array, 
    Object^ value, 
    int startIndex
)
static member IndexOf : 
        array:Array * 
        value:Object * 
        startIndex:int -> int 
public static function IndexOf(
    array : Array, 
    value : Object, 
    startIndex : int
) : int

Parameters

  • startIndex
    Type: System. . :: . .Int32
    The starting index of the search. 0 (zero) is valid in an empty array.

Return Value

Type: System. . :: . .Int32
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.

Exceptions

Exception Condition
ArgumentNullException

array is null Nothing nullptr unit a null reference (Nothing in Visual Basic) .

ArgumentOutOfRangeException

startIndex is outside the range of valid indexes for array.

RankException

array is multidimensional.

Remarks

The one-dimensional Array is searched forward starting at startIndex and ending at the last element.

The elements are compared to the specified value using the Object..::..Equals method. If the element type is a nonintrinsic (user-defined) type, the Equals implementation of that type is used.

Since most arrays will have a lower bound of zero, this method would generally return –1 when value is not found. In the rare case that the lower bound of the array is equal to Int32..::..MinValue and value is not found, this method returns Int32..::..MaxValue, which is System.Int32.MinValue - 1.

Passing the Length of the array as the startindex will result in a return value of -1, while values greater than Length will raise an ArgumentOutOfRangeException.

This method is an O(n) operation, where n is the number of elements from startIndex to the end of array.

.NET Framework Security

See Also

Reference

Array Class

IndexOf Overload

System Namespace