views:

200

answers:

6

Hi everyone,

I'm in the process of learning Interfaces. I've been reading through some books/articles and so far so good - I've written a few sample Interfaces of my own. Yay :)

Now, I've noticed that one of the most popular C# Interfaces around is the IEnumerable Interface. It's used quite a lot for all sorts of collections etc..

Anyway, I'd like to examine it, with intent to further understand how it actually works. I've searched Google but I can't seem to find a reference to the actual source code, but I imagine it would contain the Interface itself, and a class(es) containing the various Methods.

So, is anyone able to help?

Much appreciated

+2  A: 

Interfaces don't contain code.

Brian
They don't contain implementations, they contain some code.
Roman A. Taycher
So you're saying the built in methods for IEnumerable are blank?
Marko
@Marko, that's what an interface is. A definition of a contract, nothing more. *You* provide the implementations.
Anthony Pegram
Though this is partially true, it is quite rude. Technically speaking, **THERE IS CODE**. I specifically mentioned **the interface itself AND the methods it uses**. Last time I checked, this site was for helping other users, not for being cheeky.
Marko
@Marko, you need to understand an interface *uses no methods.* It *uses* nothing. *Uses* implies implementation. There is none. An interface *defines* methods, *defines* requirements via method signatures. It is a *contract* that *you* must fulfill. Specifically to `IEnumerable`, it defines precisely one method that returns an `IEnumerator` and takes no arguments. As a developer, you get to decide how that method actually works, and what other methods will be used in that process.
Anthony Pegram
@Marko Brian's point is that you can see the interface simply by looking at it (for example, right-clicking in Visual Studio and going to "go to definition"). The contract is all that's there, decompiling it will not show you anything more.
Rex M
@Marko, but to give you the benefit of the doubt, the question you actually meant was *probably* to see source code of *implementations* of `IEnumerable` and `IEnumerable<T>` and also methods that *extend* those interfaces, such as LINQ.
Anthony Pegram
Thanks @Anthony and @Rex M. I do understand that Interfaces are contracts that define requirements. I guess what I was trying to ask is, what the definitions for the methods/extension methods are for the IEnumerable interface, such as the GetEnumerator. @Joel's answer has helped a lot in explaining how it all works, especially the relationship between IEnumerable and IEnumerator and the fact that the rest is extension methods. I just think this answer was a little rude, specifically because I mentioned `and it's methods`
Marko
@Marko - I think you mis-understood my extension method comment. I reprhased the paragraph a bit to clarify.
Joel Coehoorn
@Joel - Thank you for clarifying, it did make sense first time around though.
Marko
+6  A: 

IEnumerable<T> is merely an interface that defines:

public interface IEnumerable<T> : IEnumerable
{
    // Methods
    IEnumerator<T> GetEnumerator();
}

Most of the methods I assume you want to investigate come from a bunch of extension methods that mainly live in System.Linq namespace in System.Core.dll. Take a look with Reflector.

spender
Thank you spender, I'll download Reflector and start investigating. This is all I needed! :)
Marko
+1  A: 

Look for yourself.

Download Reflector and use it to browse the .NET framework itself.

Reflector can show you what the source for IEnumerable, IEnumerable<T> and Enumerable looks like, in your choice of language (it can decompile to C# or VB or Delphi or ...)

Bevan
A: 

you should firstly understand the design pattern underneath the implemantation of any Interface, then you could deeply understand it. THe IEnumerable interface implemenet Iterator design pattern. Sometime you will use Iterator with Composite pattern

coolkid
I don't understand why I have -1 vote?
coolkid
How is it that IEnumerable is related to a Composite pattern? In my understanding, the Composite pattern's Composite and child/leaf classes must share a common interface.
smartcaveman
yes, I answer so quickly so I forget to review it. In addition, each time I use Composite pattern, I also use IEnumerable. So I hastily think that they relate. I will edit my answer, thanks for comment
coolkid
+4  A: 

IEnumerable is pretty simple:

[ComVisibleAttribute(True)]
[GuidAttribute("496B0ABE-CDEE-11d3-88E8-00902754C43A")]
public interface IEnumerable
{
    IEnumerator GetEnumerator();
}

And for completeness, IEnumerator

[ComVisibleAttribute(true)]
[GuidAttribute("496B0ABF-CDEE-11d3-88E8-00902754C43A")]
public interface IEnumerator
{
    Object Current {get;}
    bool MoveNext();
    void Reset();
}

But more often what you're really seeing in code samples is IEnumerable<T>

public interface IEnumerable<out T> : IEnumerable
{
    IEnumerator<T> GetEnumerator();
    IEnumerator GetEnumerator(); //inherited from IEnumerable
}

And again for completeness, IEnumerator<T> (with IDisposable):

public interface IEnumerator<out T> : IDisposable, IEnumerator
{

    void Dispose(); //inherited from IDsiposable
    Object Current {get;} //inherited from IEnumerator
    T Current {get;}
    bool MoveNext(); //inherited from IEnumerator
    void Reset(); //inherited from IEnumerator
}

[ComVisibleAttribute(true)]
public interface IDisposable
{
    void Dispose();
}

That's really all there is to it. There is no direct implementation code for any of this that's specific to IEnumerable or the related code show here. Rather, types like arrays or List<T> will inherit from IEnumerable and implement the required methods. Everything else beyond that is done via extension methods.

What makes it all even more powerful are these items:

  1. C#'s foreach keyword supports looping over anything that has a GetEnumerator() method that returns an IEnumerator, and therefore you can use any type that implements IEnumerable with a foreach loop.
  2. The linq query comprehension syntax works in a similar way, such that you can use most any IEnumerable with linq to objects.
  3. The yield keyword allows you to create something called iterator blocks. Iterator blocks have some neat properties (like lazy evaluation) and allow you easily create your own IEnumerable types without having to go to all the trouble of defining a new class first.

Finally, it's worth pointing out here that IDisposable is another interface worth further study, as it is used quite a bit in the framework and has direct language support similar to IEnumerable's foreach with the using keyword.

Joel Coehoorn
Thank you so much for this, my next question will probably be IDisposable related :)
Marko
It's also worth adding a minor detail here that while `IEnumerable` doesn't inherit `IDisposable` (unlike `IEnumerable<T>` which does), the code for `foreach` will do the equivalent of `if(enumerator is IDisposable){((IDisposable)enumerator).Dispose();}` so such objects will still be disposed if possible. Adding `IDisposable` support when `IEnumerable<T>` was created made this more efficient and more explicit to the coder, but the equivalent functionality was always there.
Jon Hanna
A: 

Note the Mono Implementation(I'd recommend checking it out, and use a search tool to search for ClassName.cs as it can be a little hard to find some things) is good for looking up source code

Mono IEnumerable

Microsoft also releases the source code as Shared Source(Look but don't touch/copy) copy of .net but I'd stay away from it especially if you might ever reimplement part of it or implement similar classes.

Roman A. Taycher