Possible Duplicate:
Why is there not a ForEach extension method on the IEnumerable interface?
Why ForEach extension method is not available in ObservableCollections class while it is available in List ?
Possible Duplicate:
Why is there not a ForEach extension method on the IEnumerable interface?
Why ForEach extension method is not available in ObservableCollections class while it is available in List ?
As JJohn says, it's by design. Read this post by Eric Lippert for more information. A standard foreach
loop works just as well and is generally more readable.
ForEach
is available only in List<T>
and not in any other collection. It is just design decision.
Foreach can be used only on those objects which are of classes that implements IEnumerable. IEnumerable is an interface that defines one method GetEnumerator which returns an IEnumerator interface, this in turn allows readonly access to a collection. A collection that implements IEnumerable can be used with a foreach statement.