Searches the specified one-dimensional Array, returning the index of the first occurrence of the specified object.
A int containing the index of the first occurrence of value in array, if found; otherwise, array.GetLowerBound(0) - 1.Note: For a vector, if value is not found, the return value will be -1. This provides the caller with a standard code for a failed search.
Type Reason ArgumentNullException array is null. RankException array has more than one dimension.
This version of Array.IndexOf(Array, object) is equivalent to Array.IndexOf(Array, object)(array, value, array.GetLowerBound(0),array.Length).
The elements are compared using object.Equals(object).
The following example demonstrates the Array.IndexOf(Array, object) method.
C# Example
using System; public class ArrayIndexOfExample { public static void Main() { int[] intAry = { 0, 1, 2, 0, 1 }; Console.Write( "The values of the array are: " ); foreach( int i in intAry ) Console.Write( "{0,5}", i ); Console.WriteLine(); int j = Array.IndexOf( intAry, 1 ); Console.WriteLine( "The first occurrence of 1 is at index {0}", j ); } }The output is
The values of the array are: 0 1 2 0 1
The first occurrence of 1 is at index 1
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0