IndexOf Method (Array, Object)

Array.IndexOf Method (Array, Object)


Searches for the specified object and returns the index of its first occurrence in a one-dimensional array.

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

public static int IndexOf(
	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 first occurrence of valueinarray, if found; otherwise, the lower bound of the array minus 1.

Exception Condition

array is null.


array is multidimensional.

This method searches all the elements of a one-dimensional arrayfor value. To determine whether value exists in array, the method performs an equality comparison by calling each element's Equals method until it finds a match. This means that if the element overrides the Object.Equals(Object) method, that override is called.

Because most arrays have a lower bound of zero, this method generally returns –1 ifvalue isn’t found. In the rare case that the lower bound of the array is equal to Int32.MinValue(0x80000000) and value isn’t found, this method returns Int32.MaxValue (0x7FFFFFFF).

This method is an O(n) operation, where n is the Length of array.

The example calls the following three overloads of the IndexOfmethod to find the index of a string in a string array:

  • IndexOf(Array, Object), to determine the first occurrence of the string "the" in a string array.

  • IndexOf(Array, Object, Int32), to determine the first occurrence of the string "the" in the fourth to the last elements of a string array.

  • IndexOf(Array, Object, Int32, Int32), to determine the first occurrence of the string "the" in a string array from the element that follows the last successful match to the end of the array.

public static OleDbDataAdapter CreateCustomerAdapter(
    OleDbConnection connection)
    OleDbDataAdapter adapter = new OleDbDataAdapter();
    OleDbCommand command;

    // Create the SelectCommand.
    command = new OleDbCommand("SELECT CustomerID FROM Customers " +
        "WHERE Country = ? AND City = ?", connection);

    command.Parameters.Add("Country", OleDbType.VarChar, 15);
    command.Parameters.Add("City", OleDbType.VarChar, 15);

    adapter.SelectCommand = command;

    // Create the InsertCommand.
    command = new OleDbCommand(
        "INSERT INTO Customers (CustomerID, CompanyName) " +
        "VALUES (?, ?)", connection);

        "CustomerID", OleDbType.Char, 5, "CustomerID");
        "CompanyName", OleDbType.VarChar, 40, "CompanyName");

    adapter.InsertCommand = command;
    return adapter;

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
© 2015 Microsoft