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;
}
If products is of type
List<Product>you can just use theIndexOf()method of your collection: