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
How to find the index of an item in a C# list in a single step?
To find the index of an item in a C# list in a single step, you can use several built-in methods. The most common approaches are IndexOf() for exact matches and FindIndex() for condition-based searches.
Syntax
For exact item matching −
int index = list.IndexOf(item);
For condition-based searching −
int index = list.FindIndex(predicate);
Using IndexOf() for Exact Matches
The IndexOf() method returns the zero-based index of the first occurrence of the specified item −
using System;
using System.Collections.Generic;
public class Program {
public static void Main() {
List<string> sports = new List<string> {
"Football",
"Soccer",
"Tennis",
"Basketball"
};
// Find exact match
int index = sports.IndexOf("Tennis");
Console.WriteLine("Tennis found at index: " + index);
// Item not found returns -1
int notFound = sports.IndexOf("Cricket");
Console.WriteLine("Cricket found at index: " + notFound);
}
}
The output of the above code is −
Tennis found at index: 2 Cricket found at index: -1
Using FindIndex() for Conditional Searches
The FindIndex() method uses a predicate to find items based on custom conditions −
using System;
using System.Collections.Generic;
public class Program {
public static void Main() {
List<string> sports = new List<string> {
"American Football",
"Association Soccer",
"Lawn Tennis",
"Street Basketball"
};
// Find item containing specific text
int index1 = sports.FindIndex(s => s.Contains("Tennis"));
Console.WriteLine("Item containing 'Tennis' found at index: " + index1);
// Find item starting with specific text
int index2 = sports.FindIndex(s => s.StartsWith("Street"));
Console.WriteLine("Item starting with 'Street' found at index: " + index2);
// Find item with specific length
int index3 = sports.FindIndex(s => s.Length > 15);
Console.WriteLine("First item with length > 15 found at index: " + index3);
}
}
The output of the above code is −
Item containing 'Tennis' found at index: 2 Item starting with 'Street' found at index: 3 First item with length > 15 found at index: 0
Using FindIndex() with Custom Objects
You can also use FindIndex() to search within custom objects −
using System;
using System.Collections.Generic;
public class Player {
public string Name { get; set; }
public int Score { get; set; }
}
public class Program {
public static void Main() {
List<Player> players = new List<Player> {
new Player { Name = "Alice", Score = 95 },
new Player { Name = "Bob", Score = 87 },
new Player { Name = "Charlie", Score = 92 }
};
// Find player by name
int index1 = players.FindIndex(p => p.Name == "Bob");
Console.WriteLine("Bob found at index: " + index1);
// Find player by score condition
int index2 = players.FindIndex(p => p.Score > 90);
Console.WriteLine("First player with score > 90 found at index: " + index2);
}
}
The output of the above code is −
Bob found at index: 1 First player with score > 90 found at index: 0
Comparison
| Method | Use Case | Return Value |
|---|---|---|
| IndexOf() | Exact item matching | Index of first match or -1 |
| FindIndex() | Condition-based searching | Index of first match or -1 |
| LastIndexOf() | Find last occurrence of exact item | Index of last match or -1 |
Conclusion
Use IndexOf() for finding exact item matches and FindIndex() for condition-based searches in C# lists. Both methods return -1 when no match is found, making them reliable for single-step index operations.
