1

Why would anyone use IEnumerable? What's missing in the List<>?

If every list implement the IEnumerable, so why would you want to implement it by yourself?

Can you give me a real life example?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Noam B.
  • 3,120
  • 5
  • 25
  • 38
  • 2
    The first example that comes to mind - when you want to expose a read-only collection as a property of your class. – Zohar Peled Feb 21 '16 at 12:54
  • 3
    List already implements IEnumerable, don't implement it again. The key is to expose the minimum surface area to other code. IEnumerable is pretty safe, other code can do very little damage to the state of your program. List or IList is not, other code calling Add() without you knowing about it causes bugs. – Hans Passant Feb 21 '16 at 12:59

0 Answers0