IndexOf is returning 1 for the last element of the list. Why?
This code is not used by me . I am just giving you guys an idea about my asked context through the code mentioned below
E.g. List:
List<int> abc = new List<int>();
abc.Add(1);
abc.Add(2);
abc.Add(41);
// i.e index 0 hold 1 index 1 hold 2 and index 3 hold 41
//abc[0] = 1;
//abc[1] = 2;
//abc[3] = 41;
then checking index like:
foreach (int i in abc)
{
//for first two index[0,1] IndexOf() works fine but when foreach loop hole i=41 then IndexOf() returns 1 i.e is index=1 why??
int index = abc.IndexOf(i);
}
List.IndexOf() method returns first occurrence of element (item) within the list if an element is found. If your list contains duplicate element then you may continue your search into the list using List.IndexOf(item,index).