Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to loop through a collection that supports IEnumerable?

How to loop through a collection that supports IEnumerable?

like image 642
mrblah Avatar asked Oct 07 '09 16:10

mrblah


People also ask

How do you iterate through a list in C#?

Using foreach Statement The standard option to iterate over the List in C# is using a foreach loop. Then, we can perform any action on each element of the List. The following code example demonstrates its usage by displaying the contents of the list to the console.

Can IEnumerable be modified?

As mentioned in the existing answers, an IEnumerable collection cannot be modified by trying to assign a new value to one of its elements.

Is enumerable a collection?

IEnumerable<T> is the base interface for collections in the System. Collections. Generic namespace such as List<T>, Dictionary<TKey,TValue>, and Stack<T> and other generic collections such as ObservableCollection<T> and ConcurrentStack<T>.

Why do we use IEnumerable in C#?

IEnumerable in C# is an interface that defines one method, GetEnumerator which returns an IEnumerator interface. This allows readonly access to a collection then a collection that implements IEnumerable can be used with a for-each statement.


1 Answers

A regular for each will do:

foreach (var item in collection) {     // do your stuff    } 
like image 97
Fredrik Mörk Avatar answered Sep 28 '22 08:09

Fredrik Mörk