IndexOf ExamplesΒΆ

The general syntax for this method is:

Array.IndexOf(arrayName, item)

This method returns the index of the FIRST occurence of an item in the array. If the item is not in the array, -1 is returned.

Example

1
2
3
4
5
int[] charles = new int[] {1, 7, 5, 9, 5};
string[] otherArray = {"hello", "world!"};

Console.WriteLine(Array.IndexOf(charles, 5));
Console.WriteLine(Array.IndexOf(otherArray, "hi"));

Output

2
-1