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


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

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 22-Jun-2020

576 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements