0

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 ?

Community
  • 1
  • 1
Shantanu Gupta
  • 20,688
  • 54
  • 182
  • 286
  • As it's more of a design issue question, I think it's closer to http://stackoverflow.com/questions/800151/why-is-foreach-on-ilistt-and-not-on-ienumerablet – Gareth Wilson Jun 10 '12 at 14:37

3 Answers3

6

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.

Gareth Wilson
  • 855
  • 6
  • 5
2

ForEach is available only in List<T> and not in any other collection. It is just design decision.

JJohn
  • 175
  • 2
  • 9
  • Don't you think this is a common function and this function must have been in this class. I doubt, there must be performance related issue. – Shantanu Gupta Jun 10 '12 at 14:19
  • Its not that its available only in List. You can implement you own class which implements IEnumerable and you will get ForEach functionality in your class's objects. – Saurabh R S Jun 10 '12 at 17:07
0

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.

Saurabh R S
  • 3,037
  • 1
  • 34
  • 44