Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IndexOf predicate?

I want to find the index of an element in a list maching a certain predicate, is there a better way to do it than:

var index = list.IndexOf(list.Find(predicate)); 

?

like image 669
Andreas Brinck Avatar asked Jan 13 '11 13:01

Andreas Brinck


People also ask

What is FindIndex in c#?

FindIndex Method is used to search for an element that matches the conditions defined by a specified predicate and returns the index of the first occurrence within the List<T>. If an item which matches the conditions is not found then this method will return -1.

How to use List FindIndex in c#?

To get the index of an item in a single line, use the FindIndex() and Contains() method. int index = myList. FindIndex(a => a.

What is the difference between FindIndex and IndexOf?

findIndex - Returns the index of the first element in the array where predicate is true, and -1 otherwise. indexOf - Returns the index of the first occurrence of a value in an array.

Do lists have indexes C#?

The IndexOf method returns the first index of an item if found in the List. C# List<T> class provides methods and properties to create a list of objects (classes). The IndexOf method returns the first index of an item if found in the List.


1 Answers

Are you looking for List<T>.FindIndex(predicate)?

like image 168
Jon Skeet Avatar answered Sep 27 '22 18:09

Jon Skeet