Working of foreach: As I know,
foreach is a loop which iterates through a collection or array one by one, starting from 0 index till the last item of the collection.
So, if I have n items in an array.
foreach (var item in arr)
{
}
then,
In, 1st iteration, item=arr[0];
then, in 2nd, item=arr[1];
.
.
.
in last (nth), item=arr[n-1];
Conclusion: from working it seems that at each iteration it knows that which value to be taken from array or it knows the index of the item to be taken from array.
Now my question: How can I get index of an item without using a new variable?
foreach (string item in mylist)
{
if (item == "myitem")
{
// get index of item
break;
}
}