Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# program to find the index of an element in a List
In C#, you can find the index of an element in a List<T> using the IndexOf() method. This method returns the zero-based index of the first occurrence of the specified element, or -1 if the element is not found.
Syntax
Following is the syntax for the IndexOf() method −
int IndexOf(T item) int IndexOf(T item, int startIndex) int IndexOf(T item, int startIndex, int count)
Parameters
- item: The element to locate in the list.
- startIndex: The zero-based starting index of the search (optional).
- count: The number of elements to search (optional).
Return Value
The method returns an int representing the zero-based index of the element. If the element is not found, it returns -1.
Using IndexOf() to Find Element Index
Example
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 −
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 of 68: " + index);
// finding index of non-existing element
int notFound = val.IndexOf(100);
Console.WriteLine("Index of 100: " + notFound);
}
}
The output of the above code is −
Index of 68: 2 Index of 100: -1
Using IndexOf() with String List
Example
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
List<string> fruits = new List<string> {"apple", "banana", "cherry", "banana"};
// Find first occurrence of "banana"
int index1 = fruits.IndexOf("banana");
Console.WriteLine("First occurrence of 'banana': " + index1);
// Find "banana" starting from index 2
int index2 = fruits.IndexOf("banana", 2);
Console.WriteLine("'banana' starting from index 2: " + index2);
// Element not found
int index3 = fruits.IndexOf("orange");
Console.WriteLine("Index of 'orange': " + index3);
}
}
The output of the above code is −
First occurrence of 'banana': 1 'banana' starting from index 2: 3 Index of 'orange': -1
Using IndexOf() with Custom Objects
Example
using System;
using System.Collections.Generic;
public class Person {
public string Name { get; set; }
public int Age { get; set; }
public override bool Equals(object obj) {
if (obj is Person other) {
return Name == other.Name && Age == other.Age;
}
return false;
}
public override int GetHashCode() {
return Name.GetHashCode() ^ Age.GetHashCode();
}
}
public class Demo {
public static void Main() {
List<Person> people = new List<Person> {
new Person { Name = "John", Age = 25 },
new Person { Name = "Jane", Age = 30 },
new Person { Name = "Bob", Age = 35 }
};
Person searchPerson = new Person { Name = "Jane", Age = 30 };
int index = people.IndexOf(searchPerson);
Console.WriteLine("Index of Jane (30): " + index);
}
}
The output of the above code is −
Index of Jane (30): 1
Conclusion
The IndexOf() method in C# Lists provides a simple way to find the zero-based index of any element. It returns -1 when the element is not found, making it easy to check for element existence. For custom objects, you need to override Equals() and GetHashCode() methods for proper comparison.
