Normally I do iterations using the control statement for
and thus I get the index of the iteration easily, but the question I have is the following, Is there a way to get the index of the iteration in an foreach
en c#
?
since I usually get it out like this:
int i=0;
foreach (var item in array)
{
i++;
}
The other question would be, if there is a way to get the index in a different way than the example, what is it?
If you are looping through the elements to create a new list of them, you can use the extension method
Select
( documentation ) which has an index overload.I think that the proper way to get the index in a
foreach
is the one you are using because it is a simple and optimal code and there are times when it can be interesting to use it.An example of this is with the ConcurrentBag class
In this case, it doesn't make sense to use the sequence
for
because, among other things, elements could be added/removed from another thread while traversing the list.Lastly, what you should never do is use
Array.IndexOf
to get the index because you are going to force a lookup on the list for each iterationAs an addition to what everyone else has written, I want to write a bit of theory.
for
: It is a loop that iterates within awholeset of elements, either a list, or aArray
(Generic example) :In each of its iterations it will find the value of the element
i
in theArray
, causing the following result:As you can see, yes, every element inside the
Array
.With
foreach
this it could be translated to:Which would produce the same output, now we're talking about
Array
, an array is just a set of finite elements, if we target aList<T>
, things change:In a class whose main dependency is an iterator, why not use
foreach
to access each element of theList<T>
? If we mention the performance, we are in nothing, the classList
is prepared for "fire" in the .NET Framework and the cycleforeach
too, if not, test the speed in each one. (Another generic example) :(Run the previous example several times to check the results obtained)
Now, if what you want is to obtain the index of an element in a class that contains iterators or supports
index
, it is best to use its indexer or call its respective functionIndexOf
(As Asier Villanueva and rsciriano mention )IndexOf
: It is a function that returns an integer with the current position of an element in your list, it is worth mentioning that this function is only available for collections that are in the namespaceSystem.Collections.Generic
, it looks more or less like this:Which is practically the same as iterating the list or collection yourself to get the index of the current element, the practical uses of this function come when you need to get a specific element, generic example :
And I need to specifically retrieve my name, if it exists:
The value of
IndexDeOp
is3
, so if you don't need to iterate through the list, this is your best option.As a last mention, the cycle
foreach
can only be usedin classes or "collections" that implementIEnumerable[<T>]
, but this is not entirely necessary, it is enough that the following functions and properties are implemented:GetEnumerator
,Current
andMoveNext
.Here are some reference links:
for
,foreach
, a fiddle .You can get the index through the IndexOf method of the Array class:
But the most logical thing is that if you need the index you use a for.
I would use another type of loop, since with Foreach, you need to add a counter. You can still use Foreach and a separate counter.
I would use a while loop
The correct thing would be using a for like this:
The Blucle
foreach
is used in iterations in which it is not necessary to obtain or work with the index but with the objects, to work with the index it uses the loopfor