What does the source code for IEnumerable look like?
I'm in the process of l开发者_JS百科earning 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
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:
- C#'s
foreach
keyword supports looping over anything that has aGetEnumerator()
method that returns an IEnumerator, and therefore you can use any type that implements IEnumerable with a foreach loop. - The linq query comprehension syntax works in a similar way, such that you can use most any IEnumerable with linq to objects.
- 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.
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.
Interfaces don't contain code.
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 ...)
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.
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
精彩评论