Array.IndexOf Method (Array, Object, Int32)
Searches for the specified object in a range of elements of a one-dimensional array, and returns the index of its first occurrence. The range extends from a specified index to the end of the array.
Assembly: mscorlib (in mscorlib.dll)
- Type: System.Array
The one-dimensional array to search.
- Type: System.Object
The object to locate in array.
- Type: System.Int32
The starting index of the search. 0 (zero) is valid in an empty array.
Return ValueType: System.Int32
The index of the first occurrence of value, if it’s found, within the range of elements in array that extends from startIndex to the last element; otherwise, the lower bound of the array minus 1.
This method searches a one-dimensional array from the element at index startIndex to the last element. To determine whether value exists in array, the method performs an equality comparison by calling the Equals method of every element 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 if value 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 number of elements from startIndex to the end of array.
The example calls the following three overloads of the IndexOf method 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.
, 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.
.NET FrameworkSupported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0
.NET Framework Client ProfileSupported in: 4, 3.5 SP1
Portable Class LibrarySupported in: Portable Class Library
.NET for Windows Store appsSupported in: Windows 8
.NET for Windows Phone appsSupported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8
Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.