How to find the index of an item in a C# list in a single step?

CsharpProgrammingServer Side Programming

To get the index of an item in a single line, use the FindIndex() and Contains() method.

int index = myList.FindIndex(a => a.Contains("Tennis"));

Above, we got the index of an element using the FindIndex(), which is assisted by Contains method for that specific element.

Here is the complete code −


 Live Demo

using System;
using System.Collections.Generic;
public class Program {
   public static void Main() {
      List < string > myList = new List < string > () {

      // finding index
      int index = myList.FindIndex(a => a.Contains("Tennis"));

      // displaying index
      Console.WriteLine("List Item Tennis Found at index: " + index);


List Item Tennis Found at index: 2
Published on 21-Aug-2018 12:44:40