Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Array.LastIndexOf Method (Array, Object, Int32)

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

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

static member LastIndexOf : 
        array:Array * 
        value:Object * 
        startIndex:int -> int

Parameters

array
Type: System.Array

The one-dimensional Array to search.

value
Type: System.Object

The object to locate in array.

startIndex
Type: System.Int32

The starting index of the backward search.

Return Value

Type: System.Int32
The index of the last occurrence of value within the range of elements in array that extends from the first element to startIndex, if found; otherwise, the lower bound of the array minus 1.

ExceptionCondition
ArgumentNullException

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

ArgumentOutOfRangeException

startIndex is outside the range of valid indexes for array.

RankException

array is multidimensional.

The one-dimensional Array is searched backward starting at startIndex and ending at the first 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.

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

In the .NET Framework version 2.0, this method uses the Equals and CompareTo methods of the Array to determine whether the Object specified by the value parameter exists. In the earlier versions of the .NET Framework, this determination was made by using the Equals and CompareTo methods of the value Object itself.

The following code example shows how to determine the index of the last occurrence of a specified element in an array.

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Show:
© 2015 Microsoft