- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
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
Advertisements