C# program to find the index of an element in a List

CsharpProgrammingServer Side Programming

Set a list and add elements −

List<int> val = new List<int>();

// integer elements
val.Add(35);
val.Add(55);
val.Add(68);

Let’s say now we need to find the index of element 68. For that, use the IndexOf() method −

int index = val.IndexOf(68);

Here is the complete code −

Example

 Live Demo

using System;
using System.Collections.Generic;
public class Demo {
   public static void Main() {
      List<int> val = new List<int>();
      // integer elements
      val.Add(35);
      val.Add(55);
      val.Add(68);
      // finding the index of element 68
      int index = val.IndexOf(68);
      Console.WriteLine(index);
   }
}

Output

2
raja
Published on 04-Sep-2018 12:29:24
Advertisements