Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find the index of an element inside of my List<int>?

Tags:

c#

list

Here's what I have:

public int FindItem(int elementoABuscar)
{
    int indice = vectorNumeros.FindIndex(0, asd);
    return indice;
}

I can't seem to figure out the second part of the FindIndex method.

How would I find the index of elementoABuscar?


2 Answers

Just use List<int>.IndexOf:

int indice = vectorNumeros.IndexOf(elementoABuscar);

FindIndex is used to find an item which matches a particular predicate - you don't need it here, if you're just trying to find an element directly.

Note that if your list is sorted, you could also use List<T>.BinarySearch.

like image 162
Jon Skeet Avatar answered Dec 05 '25 01:12

Jon Skeet


Why don't you use list.IndexOf(object) ?

like image 45
thelost Avatar answered Dec 05 '25 01:12

thelost



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!