Hi...
What is the Difference between Enumerable and IEnumerator?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
jnmsyPosted Apr 24, 2012, 1:19 AM
The following video may help you..
http://bestdotnettrainers.blogspot.in/search?updated-min=2012-01-01T00:00:00-08:00&updated-max=2013-01-01T00:00:00-08:00&max-results=11
VulpesPosted Apr 23, 2012, 8:59 AM
SenthilkumarPosted Apr 23, 2012, 8:21 AM
Please refer these urls:
http://www.itorian.com/articles/c-sharp/post/339/Difference-between-IEnumerable-and-IEnumerator---By-Shivprasad-Koirala.aspx
http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/6b7ca838-7afc-4223-9a73-41769c496745/
http://www.codeproject.com/Articles/4074/Using-IEnumerator-and-IEnumerable-in-the-NET-Frame
Kunal VaishyaPosted Apr 23, 2012, 7:59 AM
Implementing IEnumerable and IEnumerator
Working with a foreach loop is the primary reason to implement the IEnumerable and IEnumerator interfaces. You'll want one of each of these to work with the loop.
I am going to do an example DateRange class which will implement IEnumerable
Note: I am aware of the fact that I could achieve the same result with a for loop. I find the foreach loop more readable.
First we need to create a basic DateRange class. A range can be defined as a StartDate and an EndDate, so I'll start there.
public class DateRange { public DateRange(DateTime startDate, DateTime endDate) { StartDate = startDate; EndDate = endDate; } public DateTime StartDate { get; set; } public DateTime EndDate { get; set; } }
So this DateRange could be useful on its own, but we want to be able to iterate this collection using a foreach. So to start we need to implement the IEnumerable
public class DateRange : IEnumerable
Notice here that we now need to get the IEnumerator
public class DateRangeEnumerator : IEnumerator
These are the handful of methods we implement for the IEnumerator
Keep in mind here that I could have used a collection for this, but I didn't because I don't need one. The calculation to get the items was easy enough.
var dateRange = new DateRange(DateTime.Today.AddDays(-6), DateTime.Today); foreach (DateTime date in dateRange) { Console.WriteLine(date.ToShortDateString()); }
Need More Information Then Refer this Link
http://brendan.enrick.com/post/implementing-ienumerable-and-ienumerator.aspx