Array.IndexOf Method

Definition

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.

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[], 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[], 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[], T)

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

IndexOf(Array, Object)

Source:
Array.cs
Source:
Array.cs
Source:
Array.cs

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

public static int IndexOf (Array array, object value);
public static int IndexOf (Array array, object? value);

Parameters

array
Array

The one-dimensional array to search.

value
Object

The object to locate in array.

Returns

The 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:

// Create a string array with 3 elements having the same value.
String[] strings = { "the", "quick", "brown", "fox", "jumps",
                     "over", "the", "lazy", "dog", "in", "the",
                     "barn" };

// Display the elements of the array.
Console.WriteLine("The array contains the following values:");
for (int i = strings.GetLowerBound(0); i <= strings.GetUpperBound(0); i++)
   Console.WriteLine("   [{0,2}]: {1}", i, strings[i]);

// Search for the first occurrence of the duplicated value.
string searchString = "the";
int index = Array.IndexOf(strings, searchString);
Console.WriteLine("The first occurrence of \"{0}\" is at index {1}.",
                  searchString, index);

// Search for the first occurrence of the duplicated value in the last section of the array.
index = Array.IndexOf(strings, searchString, 4);
Console.WriteLine("The first occurrence of \"{0}\" between index 4 and the end is at index {1}.",
                  searchString, index);

// Search for the first occurrence of the duplicated value in a section of the array.
int position = index + 1;
index = Array.IndexOf(strings, searchString, position, strings.GetUpperBound(0) - position + 1);
Console.WriteLine("The first occurrence of \"{0}\" between index {1} and index {2} is at index {3}.",
                  searchString, position, strings.GetUpperBound(0), index);

// The example displays the following output:
//    The array contains the following values:
//       [ 0]: the
//       [ 1]: quick
//       [ 2]: brown
//       [ 3]: fox
//       [ 4]: jumps
//       [ 5]: over
//       [ 6]: the
//       [ 7]: lazy
//       [ 8]: dog
//       [ 9]: in
//       [10]: the
//       [11]: barn
//    The first occurrence of "the" is at index 0.
//    The first occurrence of "the" between index 4 and the end is at index 6.
//    The first occurrence of "the" between index 7 and index 11 is at index 10.

Remarks

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

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

IndexOf(Array, Object, Int32)

Source:
Array.cs
Source:
Array.cs
Source:
Array.cs

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.

public static int IndexOf (Array array, object value, int startIndex);
public static int IndexOf (Array array, object? value, int startIndex);

Parameters

array
Array

The one-dimensional array to search.

value
Object

The object to locate in array.

startIndex
Int32

The starting index of the search. 0 (zero) is valid in an empty array.

Returns

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.

Exceptions

array is null.

startIndex is outside the range of valid indexes for 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:

// Create a string array with 3 elements having the same value.
String[] strings = { "the", "quick", "brown", "fox", "jumps",
                     "over", "the", "lazy", "dog", "in", "the",
                     "barn" };

// Display the elements of the array.
Console.WriteLine("The array contains the following values:");
for (int i = strings.GetLowerBound(0); i <= strings.GetUpperBound(0); i++)
   Console.WriteLine("   [{0,2}]: {1}", i, strings[i]);

// Search for the first occurrence of the duplicated value.
string searchString = "the";
int index = Array.IndexOf(strings, searchString);
Console.WriteLine("The first occurrence of \"{0}\" is at index {1}.",
                  searchString, index);

// Search for the first occurrence of the duplicated value in the last section of the array.
index = Array.IndexOf(strings, searchString, 4);
Console.WriteLine("The first occurrence of \"{0}\" between index 4 and the end is at index {1}.",
                  searchString, index);

// Search for the first occurrence of the duplicated value in a section of the array.
int position = index + 1;
index = Array.IndexOf(strings, searchString, position, strings.GetUpperBound(0) - position + 1);
Console.WriteLine("The first occurrence of \"{0}\" between index {1} and index {2} is at index {3}.",
                  searchString, position, strings.GetUpperBound(0), index);

// The example displays the following output:
//    The array contains the following values:
//       [ 0]: the
//       [ 1]: quick
//       [ 2]: brown
//       [ 3]: fox
//       [ 4]: jumps
//       [ 5]: over
//       [ 6]: the
//       [ 7]: lazy
//       [ 8]: dog
//       [ 9]: in
//       [10]: the
//       [11]: barn
//    The first occurrence of "the" is at index 0.
//    The first occurrence of "the" between index 4 and the end is at index 6.
//    The first occurrence of "the" between index 7 and index 11 is at index 10.

Remarks

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).

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 the number of elements from startIndex to the end of array.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

IndexOf(Array, Object, Int32, Int32)

Source:
Array.cs
Source:
Array.cs
Source:
Array.cs

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.

public static int IndexOf (Array array, object value, int startIndex, int count);
public static int IndexOf (Array array, object? value, int startIndex, int count);

Parameters

array
Array

The one-dimensional array to search.

value
Object

The object to locate in array.

startIndex
Int32

The starting index of the search. 0 (zero) is valid in an empty array.

count
Int32

The number of elements to search.

Returns

The 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.

// Create a string array with 3 elements having the same value.
String[] strings = { "the", "quick", "brown", "fox", "jumps",
                     "over", "the", "lazy", "dog", "in", "the",
                     "barn" };

// Display the elements of the array.
Console.WriteLine("The array contains the following values:");
for (int i = strings.GetLowerBound(0); i <= strings.GetUpperBound(0); i++)
   Console.WriteLine("   [{0,2}]: {1}", i, strings[i]);

// Search for the first occurrence of the duplicated value.
string searchString = "the";
int index = Array.IndexOf(strings, searchString);
Console.WriteLine("The first occurrence of \"{0}\" is at index {1}.",
                  searchString, index);

// Search for the first occurrence of the duplicated value in the last section of the array.
index = Array.IndexOf(strings, searchString, 4);
Console.WriteLine("The first occurrence of \"{0}\" between index 4 and the end is at index {1}.",
                  searchString, index);

// Search for the first occurrence of the duplicated value in a section of the array.
int position = index + 1;
index = Array.IndexOf(strings, searchString, position, strings.GetUpperBound(0) - position + 1);
Console.WriteLine("The first occurrence of \"{0}\" between index {1} and index {2} is at index {3}.",
                  searchString, position, strings.GetUpperBound(0), index);

// The example displays the following output:
//    The array contains the following values:
//       [ 0]: the
//       [ 1]: quick
//       [ 2]: brown
//       [ 3]: fox
//       [ 4]: jumps
//       [ 5]: over
//       [ 6]: the
//       [ 7]: lazy
//       [ 8]: dog
//       [ 9]: in
//       [10]: the
//       [11]: barn
//    The first occurrence of "the" is at index 0.
//    The first occurrence of "the" between index 4 and the end is at index 6.
//    The first occurrence of "the" between index 7 and index 11 is at index 10.

Remarks

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 Equals method of every element until it finds a match. This means that if the element overrides the Object.Equals method, that override is called.

Because most arrays have a lower bound of zero, this method generally returns -1 when 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).

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

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

IndexOf<T>(T[], T, Int32)

Source:
Array.cs
Source:
Array.cs
Source:
Array.cs

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.

public static int IndexOf<T> (T[] array, T value, int startIndex);

Type Parameters

T

The type of the elements of the array.

Parameters

array
T[]

The one-dimensional, zero-based array to search.

value
T

The object to locate in array.

startIndex
Int32

The zero-based starting index of the search. 0 (zero) is valid in an empty array.

Returns

The 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[], T) method overload searches the array from the beginning, and finds the first occurrence of the string. The IndexOf<T>(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[], 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.

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
*/

Remarks

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

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

IndexOf<T>(T[], T, Int32, Int32)

Source:
Array.cs
Source:
Array.cs
Source:
Array.cs

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.

public static int IndexOf<T> (T[] array, T value, int startIndex, int count);

Type Parameters

T

The type of the elements of the array.

Parameters

array
T[]

The one-dimensional, zero-based array to search.

value
T

The object to locate in array.

startIndex
Int32

The zero-based starting index of the search. 0 (zero) is valid in an empty array.

count
Int32

The number of elements in the section to search.

Returns

The 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[], T) method overload searches the array from the beginning, and finds the first occurrence of the string. The IndexOf<T>(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[], 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.

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
*/

Remarks

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

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

IndexOf<T>(T[], T)

Source:
Array.cs
Source:
Array.cs
Source:
Array.cs

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

public static int IndexOf<T> (T[] array, T value);

Type Parameters

T

The type of the elements of the array.

Parameters

array
T[]

The one-dimensional, zero-based array to search.

value
T

The object to locate in array.

Returns

The 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[], T) method overload searches the array from the beginning, and finds the first occurrence of the string. The IndexOf<T>(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[], 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.

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
*/

Remarks

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

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0