Reputation: 6512
I'm trying to an indexof method for a class that represents a list of objects.I would like to know the best approach. below is the code that I've come up with.
public int IndexOf(Product product)
{
Product p;
for (int i = 0; i < products.Count; i++)
{
p = products[i];
if (p == product)
return i;
}
return -1;
}
Upvotes: 2
Views: 2696
Reputation: 235
I couldn't get .IndexOf() working for my problem, but I found the following solution worked a treat.
How to use IndexOf() method of List<object>
Upvotes: 1
Reputation: 160852
If products is of type List<Product>
you can just use the IndexOf()
method of your collection:
public int IndexOf(Product product)
{
return products.IndexOf(product);
}
Upvotes: 3