Hi...
What is the Difference between Enumerable and IEnumerator?
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Kunal VaishyaRank #990 · 2K reputationPosted Apr 23, 2012, 3:59 AM EDT
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
SenthilkumarRank #387 · 6.5K reputationPosted Apr 23, 2012, 4:21 AM EDT
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
VulpesRank #55 · 29.2K reputationPosted Apr 23, 2012, 4:59 AM EDT
jnmsy19 reputationPosted Apr 23, 2012, 9:19 PM EDT
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