Array.IndexOf Method
- Reference
Please rate your experience
Feedback will be sent to Microsoft: By pressing the submit button, your feedback will be used to improve Microsoft products and services. Privacy policy.
Thank you.
Definition
Important
Some information relates to prerelease product that may be substantially modified before its released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Searches for the specified object and returns the index of its first occurrence in a one-dimensional array or in a range of elements in the array.
In this article
Overloads
IndexOf[Array, Object] |
Searches for the specified object and returns the index of its first occurrence in a one-dimensional array. |
IndexOf[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. |
IndexOf[Array, Object, Int32, Int32] |
Searches for the specified object in a range of elements of a one-dimensional array, and returns the index of ifs first occurrence. The range extends from a specified index for a specified number of elements. |
IndexOf[T[], T, 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. |
IndexOf[T[], T, Int32, 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 for a specified number of elements. |
IndexOf[T[], T] |
Searches for the specified object and returns the index of its first occurrence in a one-dimensional array. |
IndexOf[Array, Object]
Searches for the specified object and returns the index of its first occurrence in a one-dimensional array.
Parameters
The one-dimensional array to search.
The object to locate in array.
Returns
Int32The index of the first occurrence of value in array, if found; otherwise, the lower bound of the array minus 1.
Exceptions
array is null.
array is multidimensional.
Examples
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.
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.
Parameters
The one-dimensional array to search.
The object to locate in array.
The starting index of the search. 0 [zero] is valid in an empty array.
The number of elements to search.
Returns
Int32The index of the first occurrence of value, if it's found in the array from index startIndex to startIndex + count - 1; otherwise, the lower bound of the array minus 1.
Exceptions
array is null.
startIndex is outside the range of valid indexes for array.
-or-
count is less than zero.
-or-
startIndex and count do not specify a valid section in array.
array is multidimensional.
Examples
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.
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. To determine the value of the count argument, it subtracts the upper bound of the array from the starting index and adds one.
Type Parameters
The type of the elements of the array.
Parameters
The one-dimensional, zero-based array to search.
The object to locate in array.
The zero-based starting index of the search. 0 [zero] is valid in an empty array.
Returns
Int32The zero-based index of the first occurrence of value within the range of elements in array that extends from startIndex to the last element, if found; otherwise, -1.
Exceptions
array is null.
startIndex is outside the range of valid indexes for array.
Examples
The following example demonstrates all three generic overloads of the IndexOf method. An array of strings is created, with one entry that appears twice, at index location 0 and index location 5. The IndexOf[T[], T] method overload searches the array from the beginning, and finds the first occurrence of the string. The IndexOf[T[], T, Int32] method overload is used to search the array beginning with index location 3 and continuing to the end of the array, and finds the second occurrence of the string. Finally, the IndexOf[T[], T, Int32, Int32] method overload is used to search a range of two entries, beginning at index location two; it returns -1 because there are no instances of the search string in that range.
using namespace System; void main[] { array^ dinosaurs = { "Tyrannosaurus", "Amargasaurus", "Mamenchisaurus", "Brachiosaurus", "Deinonychus", "Tyrannosaurus", "Compsognathus" }; Console::WriteLine[]; for each[String^ dinosaur in dinosaurs ] { Console::WriteLine[dinosaur]; } Console::WriteLine["\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\"]: {0}", Array::IndexOf[dinosaurs, "Tyrannosaurus"]]; Console::WriteLine["\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\", 3]: {0}", Array::IndexOf[dinosaurs, "Tyrannosaurus", 3]]; Console::WriteLine["\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\", 2, 2]: {0}", Array::IndexOf[dinosaurs, "Tyrannosaurus", 2, 2]]; } /* This code example produces the following output: Tyrannosaurus Amargasaurus Mamenchisaurus Brachiosaurus Deinonychus Tyrannosaurus Compsognathus Array.IndexOf[dinosaurs, "Tyrannosaurus"]: 0 Array.IndexOf[dinosaurs, "Tyrannosaurus", 3]: 5 Array.IndexOf[dinosaurs, "Tyrannosaurus", 2, 2]: -1 */ string[] dinosaurs = { "Tyrannosaurus", "Amargasaurus", "Mamenchisaurus", "Brachiosaurus", "Deinonychus", "Tyrannosaurus", "Compsognathus" }; Console.WriteLine[]; foreach[string dinosaur in dinosaurs] { Console.WriteLine[dinosaur]; } Console.WriteLine[ "\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\"]: {0}", Array.IndexOf[dinosaurs, "Tyrannosaurus"]]; Console.WriteLine[ "\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\", 3]: {0}", Array.IndexOf[dinosaurs, "Tyrannosaurus", 3]]; Console.WriteLine[ "\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\", 2, 2]: {0}", Array.IndexOf[dinosaurs, "Tyrannosaurus", 2, 2]]; /* This code example produces the following output: Tyrannosaurus Amargasaurus Mamenchisaurus Brachiosaurus Deinonychus Tyrannosaurus Compsognathus Array.IndexOf[dinosaurs, "Tyrannosaurus"]: 0 Array.IndexOf[dinosaurs, "Tyrannosaurus", 3]: 5 Array.IndexOf[dinosaurs, "Tyrannosaurus", 2, 2]: -1 */ open System let dinosaurs = [| "Tyrannosaurus" "Amargasaurus" "Mamenchisaurus" "Brachiosaurus" "Deinonychus" "Tyrannosaurus" "Compsognathus" |] printfn "" for dino in dinosaurs do printfn $"{dino}" Array.IndexOf[dinosaurs, "Tyrannosaurus"] |> printfn "\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\"]: %i" Array.IndexOf[dinosaurs, "Tyrannosaurus", 3] |> printfn "\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\", 3]: %i" Array.IndexOf[dinosaurs, "Tyrannosaurus", 2, 2] |> printfn "\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\", 2, 2]: %i" // This code example produces the following output: // // Tyrannosaurus // Amargasaurus // Mamenchisaurus // Brachiosaurus // Deinonychus // Tyrannosaurus // Compsognathus // // Array.IndexOf[dinosaurs, "Tyrannosaurus"]: 0 // // Array.IndexOf[dinosaurs, "Tyrannosaurus", 3]: 5 // // Array.IndexOf[dinosaurs, "Tyrannosaurus", 2, 2]: -1 Public Class Example Public Shared Sub Main[] Dim dinosaurs[] As String = { "Tyrannosaurus", _ "Amargasaurus", _ "Mamenchisaurus", _ "Brachiosaurus", _ "Deinonychus", _ "Tyrannosaurus", _ "Compsognathus" } Console.WriteLine[] For Each dinosaur As String In dinosaurs Console.WriteLine[dinosaur] Next Console.WriteLine[vbLf & _ "Array.IndexOf[dinosaurs, ""Tyrannosaurus""]: {0}", _ Array.IndexOf[dinosaurs, "Tyrannosaurus"]] Console.WriteLine[vbLf & _ "Array.IndexOf[dinosaurs, ""Tyrannosaurus"", 3]: {0}", _ Array.IndexOf[dinosaurs, "Tyrannosaurus", 3]] Console.WriteLine[vbLf & _ "Array.IndexOf[dinosaurs, ""Tyrannosaurus"", 2, 2]: {0}", _ Array.IndexOf[dinosaurs, "Tyrannosaurus", 2, 2]] End Sub End Class ' This code example produces the following output: ' 'Tyrannosaurus 'Amargasaurus 'Mamenchisaurus 'Brachiosaurus 'Deinonychus 'Tyrannosaurus 'Compsognathus ' 'Array.IndexOf[dinosaurs, "Tyrannosaurus"]: 0 ' 'Array.IndexOf[dinosaurs, "Tyrannosaurus", 3]: 5 ' 'Array.IndexOf[dinosaurs, "Tyrannosaurus", 2, 2]: -1Remarks
This method searches a one-dimensional array from the element at startIndex to the end of the array. To determine whether value exists in array, the method performs an equality comparison by calling the T.Equals method on every element. This means that if T overrides the Equals method, that override is called.
If startIndex equals Length,the method returns -1. If startIndex is greater than Array.Length, the method throws an ArgumentOutOfRangeException.
This method is an O[n] operation, where n is the number of elements from startIndex to the end of array.
See also
- LastIndexOf
- Performing Culture-Insensitive String Operations in Arrays
Applies to
IndexOf[T[], T, Int32, 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 for a specified number of elements.
Type Parameters
The type of the elements of the array.
Parameters
The one-dimensional, zero-based array to search.
The object to locate in array.
The zero-based starting index of the search. 0 [zero] is valid in an empty array.
The number of elements in the section to search.
Returns
Int32The zero-based index of the first occurrence of value within the range of elements in array that starts at startIndex and contains the number of elements specified in count, if found; otherwise, -1.
Exceptions
array is null.
startIndex is outside the range of valid indexes for array.
-or-
count is less than zero.
-or-
startIndex and count do not specify a valid section in array.
Examples
The following example demonstrates all three generic overloads of the IndexOf method. An array of strings is created, with one entry that appears twice, at index location 0 and index location 5. The IndexOf[T[], T] method overload searches the array from the beginning, and finds the first occurrence of the string. The IndexOf[T[], T, Int32] method overload is used to search the array beginning with index location 3 and continuing to the end of the array, and finds the second occurrence of the string. Finally, the IndexOf[T[], T, Int32, Int32] method overload is used to search a range of two entries, beginning at index location two; it returns -1 because there are no instances of the search string in that range.
using namespace System; void main[] { array^ dinosaurs = { "Tyrannosaurus", "Amargasaurus", "Mamenchisaurus", "Brachiosaurus", "Deinonychus", "Tyrannosaurus", "Compsognathus" }; Console::WriteLine[]; for each[String^ dinosaur in dinosaurs ] { Console::WriteLine[dinosaur]; } Console::WriteLine["\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\"]: {0}", Array::IndexOf[dinosaurs, "Tyrannosaurus"]]; Console::WriteLine["\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\", 3]: {0}", Array::IndexOf[dinosaurs, "Tyrannosaurus", 3]]; Console::WriteLine["\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\", 2, 2]: {0}", Array::IndexOf[dinosaurs, "Tyrannosaurus", 2, 2]]; } /* This code example produces the following output: Tyrannosaurus Amargasaurus Mamenchisaurus Brachiosaurus Deinonychus Tyrannosaurus Compsognathus Array.IndexOf[dinosaurs, "Tyrannosaurus"]: 0 Array.IndexOf[dinosaurs, "Tyrannosaurus", 3]: 5 Array.IndexOf[dinosaurs, "Tyrannosaurus", 2, 2]: -1 */ string[] dinosaurs = { "Tyrannosaurus", "Amargasaurus", "Mamenchisaurus", "Brachiosaurus", "Deinonychus", "Tyrannosaurus", "Compsognathus" }; Console.WriteLine[]; foreach[string dinosaur in dinosaurs] { Console.WriteLine[dinosaur]; } Console.WriteLine[ "\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\"]: {0}", Array.IndexOf[dinosaurs, "Tyrannosaurus"]]; Console.WriteLine[ "\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\", 3]: {0}", Array.IndexOf[dinosaurs, "Tyrannosaurus", 3]]; Console.WriteLine[ "\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\", 2, 2]: {0}", Array.IndexOf[dinosaurs, "Tyrannosaurus", 2, 2]]; /* This code example produces the following output: Tyrannosaurus Amargasaurus Mamenchisaurus Brachiosaurus Deinonychus Tyrannosaurus Compsognathus Array.IndexOf[dinosaurs, "Tyrannosaurus"]: 0 Array.IndexOf[dinosaurs, "Tyrannosaurus", 3]: 5 Array.IndexOf[dinosaurs, "Tyrannosaurus", 2, 2]: -1 */ open System let dinosaurs = [| "Tyrannosaurus" "Amargasaurus" "Mamenchisaurus" "Brachiosaurus" "Deinonychus" "Tyrannosaurus" "Compsognathus" |] printfn "" for dino in dinosaurs do printfn $"{dino}" Array.IndexOf[dinosaurs, "Tyrannosaurus"] |> printfn "\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\"]: %i" Array.IndexOf[dinosaurs, "Tyrannosaurus", 3] |> printfn "\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\", 3]: %i" Array.IndexOf[dinosaurs, "Tyrannosaurus", 2, 2] |> printfn "\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\", 2, 2]: %i" // This code example produces the following output: // // Tyrannosaurus // Amargasaurus // Mamenchisaurus // Brachiosaurus // Deinonychus // Tyrannosaurus // Compsognathus // // Array.IndexOf[dinosaurs, "Tyrannosaurus"]: 0 // // Array.IndexOf[dinosaurs, "Tyrannosaurus", 3]: 5 // // Array.IndexOf[dinosaurs, "Tyrannosaurus", 2, 2]: -1 Public Class Example Public Shared Sub Main[] Dim dinosaurs[] As String = { "Tyrannosaurus", _ "Amargasaurus", _ "Mamenchisaurus", _ "Brachiosaurus", _ "Deinonychus", _ "Tyrannosaurus", _ "Compsognathus" } Console.WriteLine[] For Each dinosaur As String In dinosaurs Console.WriteLine[dinosaur] Next Console.WriteLine[vbLf & _ "Array.IndexOf[dinosaurs, ""Tyrannosaurus""]: {0}", _ Array.IndexOf[dinosaurs, "Tyrannosaurus"]] Console.WriteLine[vbLf & _ "Array.IndexOf[dinosaurs, ""Tyrannosaurus"", 3]: {0}", _ Array.IndexOf[dinosaurs, "Tyrannosaurus", 3]] Console.WriteLine[vbLf & _ "Array.IndexOf[dinosaurs, ""Tyrannosaurus"", 2, 2]: {0}", _ Array.IndexOf[dinosaurs, "Tyrannosaurus", 2, 2]] End Sub End Class ' This code example produces the following output: ' 'Tyrannosaurus 'Amargasaurus 'Mamenchisaurus 'Brachiosaurus 'Deinonychus 'Tyrannosaurus 'Compsognathus ' 'Array.IndexOf[dinosaurs, "Tyrannosaurus"]: 0 ' 'Array.IndexOf[dinosaurs, "Tyrannosaurus", 3]: 5 ' 'Array.IndexOf[dinosaurs, "Tyrannosaurus", 2, 2]: -1Remarks
This method searches the elements of a one-dimensional array from startIndex to startIndex plus count minus 1, if count is greater than 0. To determine whether value exists in array, the method performs an equality comparison by calling the T.Equals method on every element. This means that if T overrides the Equals method, that override is called.
If startIndex equals Array.Length, the method returns -1. If startIndex is greater than Array.Length, the method throws an ArgumentOutOfRangeException.
This method is an O[n] operation, where n is count.
See also
- LastIndexOf
- Performing Culture-Insensitive String Operations in Arrays
Applies to
IndexOf[T[], T]
Searches for the specified object and returns the index of its first occurrence in a one-dimensional array.
Type Parameters
The type of the elements of the array.
Parameters
The one-dimensional, zero-based array to search.
The object to locate in array.
Returns
Int32The zero-based index of the first occurrence of value in the entire array, if found; otherwise, -1.
Exceptions
array is null.
Examples
The following example demonstrates all three generic overloads of the IndexOf method. An array of strings is created, with one entry that appears twice, at index location 0 and index location 5. The IndexOf[T[], T] method overload searches the array from the beginning, and finds the first occurrence of the string. The IndexOf[T[], T, Int32] method overload is used to search the array beginning with index location 3 and continuing to the end of the array, and finds the second occurrence of the string. Finally, the IndexOf[T[], T, Int32, Int32] method overload is used to search a range of two entries, beginning at index location two; it returns -1 because there are no instances of the search string in that range.
using namespace System; void main[] { array^ dinosaurs = { "Tyrannosaurus", "Amargasaurus", "Mamenchisaurus", "Brachiosaurus", "Deinonychus", "Tyrannosaurus", "Compsognathus" }; Console::WriteLine[]; for each[String^ dinosaur in dinosaurs ] { Console::WriteLine[dinosaur]; } Console::WriteLine["\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\"]: {0}", Array::IndexOf[dinosaurs, "Tyrannosaurus"]]; Console::WriteLine["\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\", 3]: {0}", Array::IndexOf[dinosaurs, "Tyrannosaurus", 3]]; Console::WriteLine["\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\", 2, 2]: {0}", Array::IndexOf[dinosaurs, "Tyrannosaurus", 2, 2]]; } /* This code example produces the following output: Tyrannosaurus Amargasaurus Mamenchisaurus Brachiosaurus Deinonychus Tyrannosaurus Compsognathus Array.IndexOf[dinosaurs, "Tyrannosaurus"]: 0 Array.IndexOf[dinosaurs, "Tyrannosaurus", 3]: 5 Array.IndexOf[dinosaurs, "Tyrannosaurus", 2, 2]: -1 */ string[] dinosaurs = { "Tyrannosaurus", "Amargasaurus", "Mamenchisaurus", "Brachiosaurus", "Deinonychus", "Tyrannosaurus", "Compsognathus" }; Console.WriteLine[]; foreach[string dinosaur in dinosaurs] { Console.WriteLine[dinosaur]; } Console.WriteLine[ "\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\"]: {0}", Array.IndexOf[dinosaurs, "Tyrannosaurus"]]; Console.WriteLine[ "\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\", 3]: {0}", Array.IndexOf[dinosaurs, "Tyrannosaurus", 3]]; Console.WriteLine[ "\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\", 2, 2]: {0}", Array.IndexOf[dinosaurs, "Tyrannosaurus", 2, 2]]; /* This code example produces the following output: Tyrannosaurus Amargasaurus Mamenchisaurus Brachiosaurus Deinonychus Tyrannosaurus Compsognathus Array.IndexOf[dinosaurs, "Tyrannosaurus"]: 0 Array.IndexOf[dinosaurs, "Tyrannosaurus", 3]: 5 Array.IndexOf[dinosaurs, "Tyrannosaurus", 2, 2]: -1 */ open System let dinosaurs = [| "Tyrannosaurus" "Amargasaurus" "Mamenchisaurus" "Brachiosaurus" "Deinonychus" "Tyrannosaurus" "Compsognathus" |] printfn "" for dino in dinosaurs do printfn $"{dino}" Array.IndexOf[dinosaurs, "Tyrannosaurus"] |> printfn "\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\"]: %i" Array.IndexOf[dinosaurs, "Tyrannosaurus", 3] |> printfn "\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\", 3]: %i" Array.IndexOf[dinosaurs, "Tyrannosaurus", 2, 2] |> printfn "\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\", 2, 2]: %i" // This code example produces the following output: // // Tyrannosaurus // Amargasaurus // Mamenchisaurus // Brachiosaurus // Deinonychus // Tyrannosaurus // Compsognathus // // Array.IndexOf[dinosaurs, "Tyrannosaurus"]: 0 // // Array.IndexOf[dinosaurs, "Tyrannosaurus", 3]: 5 // // Array.IndexOf[dinosaurs, "Tyrannosaurus", 2, 2]: -1 Public Class Example Public Shared Sub Main[] Dim dinosaurs[] As String = { "Tyrannosaurus", _ "Amargasaurus", _ "Mamenchisaurus", _ "Brachiosaurus", _ "Deinonychus", _ "Tyrannosaurus", _ "Compsognathus" } Console.WriteLine[] For Each dinosaur As String In dinosaurs Console.WriteLine[dinosaur] Next Console.WriteLine[vbLf & _ "Array.IndexOf[dinosaurs, ""Tyrannosaurus""]: {0}", _ Array.IndexOf[dinosaurs, "Tyrannosaurus"]] Console.WriteLine[vbLf & _ "Array.IndexOf[dinosaurs, ""Tyrannosaurus"", 3]: {0}", _ Array.IndexOf[dinosaurs, "Tyrannosaurus", 3]] Console.WriteLine[vbLf & _ "Array.IndexOf[dinosaurs, ""Tyrannosaurus"", 2, 2]: {0}", _ Array.IndexOf[dinosaurs, "Tyrannosaurus", 2, 2]] End Sub End Class ' This code example produces the following output: ' 'Tyrannosaurus 'Amargasaurus 'Mamenchisaurus 'Brachiosaurus 'Deinonychus 'Tyrannosaurus 'Compsognathus ' 'Array.IndexOf[dinosaurs, "Tyrannosaurus"]: 0 ' 'Array.IndexOf[dinosaurs, "Tyrannosaurus", 3]: 5 ' 'Array.IndexOf[dinosaurs, "Tyrannosaurus", 2, 2]: -1Remarks
This method searches all the elements of a one-dimensional array for value. To determine whether value exists in array, the method performs an equality comparison by calling the T.Equals method on every element. This means that if T overrides the Equals method, that override is called.
This method is an O[n] operation, where n is the Length of array.
See also
- LastIndexOf
- Performing Culture-Insensitive String Operations in Arrays