Array.LastIndexOf Method (Array, Object)


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

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

public static int LastIndexOf(
	Array array,
	object value


Type: System.Array

The one-dimensional Array to search.

Type: System.Object

The object to locate in array.

Return Value

Type: System.Int32

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

Exception Condition

array is null.


array is multidimensional.

The one-dimensional Array is searched backward starting at the last element 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 Length of array.

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 valueObject itself.

CompareTo methods of the item parameter on the objects in the collection.

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

public DataSet CreateCmdsAndUpdate(string connectionString,
    string queryString)
    using (OleDbConnection connection = new OleDbConnection(connectionString))
        OleDbDataAdapter adapter = new OleDbDataAdapter();
        adapter.SelectCommand = new OleDbCommand(queryString, connection);
        OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);


        DataSet customers = new DataSet();

        //code to modify data in dataset here


        return customers;

Universal Windows Platform
Available since 4.5
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
Return to top
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2015 Microsoft