7 Replies - 1025 Views - Last Post: 25 October 2011 - 07:44 AM Rate Topic: -----

#1 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,693
  • Joined: 13-March 10

IEnumerable - whats the point

Posted 23 October 2011 - 01:28 PM

I was reading about IEnumerable + IEnumerator and my question is why bother?
E.g.
We have a Car class.
We have a Garage class containing a lot of Cars.

To traverse Garage cars we could do:

1)Make Garage implement IEnumerable.
2)Provide Garage with property that will give us back collection of Cars.

What are differences + advantages/disadvantages of the 2 methods.

Thanks for your time.

This post has been edited by darek9576: 23 October 2011 - 01:28 PM


Is This A Good Question/Topic? 0
  • +

Replies To: IEnumerable - whats the point

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Re: IEnumerable - whats the point

Posted 23 October 2011 - 01:40 PM

A read-only immutable view through the collection.
Plus you get for free, of the extension method available on IEnumerable(Of T),

Oh then there LINQ.
Was This Post Helpful? 1
  • +
  • -

#3 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,693
  • Joined: 13-March 10

Re: IEnumerable - whats the point

Posted 23 October 2011 - 03:26 PM

Oh. Ok. So by implementing IEnumerable we cannot modify the underlying collection and LINQ always returns IEnumberable.

Thanks for your answer.
Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Re: IEnumerable - whats the point

Posted 24 October 2011 - 07:55 AM

It's not often that you have to Implement IEnumerable as all of the Collections types implement it. So just use
{your collection}.AsEnumerable

This post has been edited by AdamSpeight2008: 24 October 2011 - 07:56 AM

Was This Post Helpful? 1
  • +
  • -

#5 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: IEnumerable - whats the point

Posted 24 October 2011 - 08:56 AM

Quote

So by implementing IEnumerable we cannot modify the underlying collection


No, that's not true. The enumerator provides a way to iterate through the collection in a read-only way, but that doesn't mean that anything implementing IEnumerable is read-only. Almost every collection implements IEnumerable, most of which are not readonly.

But when you use the exposed IEnumerator to actually iterate through the collection, that is a readonly operation. For instance, you can't do a foreach loop and add an item to the list in the middle of the loop.

The first place to start for these kinds of questions is always here:

http://msdn.microsof...y/9eekhta0.aspx

The point of IEnumerable is to provide a simple mechanism for traversing collections of all kinds. For instance, a List and a LinkedList aren't stored in memory the same way. Not at all. So how could you possibly treat them the same? Well, you do that by having them implement a common interface, that exposes a common behavior.

Because this common behavior is defined in an Interface, that means each class that implements it can implement that behavior however it wants, as long as it conforms to the Interface declaration.

So in my LinkedList, I can have the enumerator follow the Next properties until it's null, and in my List, I can have it increment a counter and return array indexes.

This means we can use constructs like foreach to abstract away the actual getting and using of IEnumerators. So if we had an array declared like this:

int[] arr = new[] { 1, 2, 3, 4, 5 };


We could write the code to iterate through it like this:

var enumerator = (arr as IEnumerable<int>).GetEnumerator();
while (enumerator.MoveNext()) {
    var i = enumerator.Current;
    Console.WriteLine(i);
}


But that's tedious. That's why we have the foreach construct, which abstracts this (it actually does basically the same thing):

foreach (var i in arr)
    Console.WriteLine(i);


And this means we can loop through any collection without understanding the mechanics of how to go from one element to the next.
Was This Post Helpful? 2
  • +
  • -

#6 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,693
  • Joined: 13-March 10

Re: IEnumerable - whats the point

Posted 24 October 2011 - 09:01 AM

Wow. Sweet. Thank you for a detailed answer.
Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Re: IEnumerable - whats the point

Posted 25 October 2011 - 06:55 AM

Just as a side note For Each binds to the ICollection pattern, not IEnumerable.
Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: IEnumerable - whats the point

Posted 25 October 2011 - 07:44 AM

For C#, it's IEnumerable/IEnumerable<T>

http://msdn.microsof...(v=VS.100).aspx

Quote

The foreach statement repeats a group of embedded statements for each element in an array or an object collection that implements the System.Collections.IEnumerable or System.Collections.Generic.IEnumerable<T> interface.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1