IndexOf MethodΒΆ

Returns the index number of specified element. If element is not in the list, returns -1.

listName.IndexOf(element);

Can also be used to verify the location of an element.

listName.IndexOf(element, index);

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
List<string> planets = new List<string>()
{
   "Mercury",
   "Venus",
   "Earth",
   "Mars",
   "Jupiter",
   "Saturn",
   "Uranus",
   "Neptune"
};

Console.WriteLine(planets.IndexOf("Jupiter"));
Console.WriteLine(planets.IndexOf("Mars", 7));
Console.WriteLine(planets.IndexOf("Pluto"));

Output

4
-1
-1