locked
Use of IEnumerable RRS feed

  • Question

  • Hi,

    If I have a collection of "Person" class and I want to get person objects using Foreach loop, I need to implement the "Person" class for IEnumerable. But I cannot understand the same thing I can do easily declaring a List<Person>. Then what advantage I am achieving while I implementing a class from IEnumerable?

    regards

    ronit_rc

    Thursday, November 10, 2011 3:49 PM

Answers

  • "IEnumerable<T> represents a series of items that you can iterate over (using foreach, for example), whereas IList<T> is a collection that you can add to or remove from."

    If all you are looping through the collection then it's probably better to use IEnumerable<T> over IList<T> especially if you are exposing an interface to other clients.

    • Marked as answer by Paul Zhou Friday, November 18, 2011 7:43 AM
    Thursday, November 10, 2011 4:03 PM

All replies

  • "IEnumerable<T> represents a series of items that you can iterate over (using foreach, for example), whereas IList<T> is a collection that you can add to or remove from."

    If all you are looping through the collection then it's probably better to use IEnumerable<T> over IList<T> especially if you are exposing an interface to other clients.

    • Marked as answer by Paul Zhou Friday, November 18, 2011 7:43 AM
    Thursday, November 10, 2011 4:03 PM
  • Hi,

     

    Has your issue been resolved? Would you mind letting us know the result of the suggestions?

     

    Now I will mark an answer, you can mark others that you think to be so useful to your issue.

    If you still have any questions about this issue, please feel free to let me know.


    Have a nice day!


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Friday, November 18, 2011 7:43 AM