From the course: C#: Design Patterns Part 1

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

Explicit iterator implementation in C# for generated data

Explicit iterator implementation in C# for generated data - Python Tutorial

From the course: C#: Design Patterns Part 1

Explicit iterator implementation in C# for generated data

- Now let's implement a simple iterator in a C Sharp console app with the IEnumerable and IEnumerator interfaces. We'll start with a new empty console project named Iterator, then I'll open that project. I'll close the welcome window and open the program file. If you get this dialogue say yes. We're going to create the iterator by implementing the interfaces we just mentioned. So we'll start by using system collections and using system collections generic. To illustrate the iterator pattern, we're going to create an iterator for the number of days in each month. This will allow us to display or count the number of days in each month with a foreach loop. We don't need to know how many months are in a year or how many years we're counting like we would if we did this in a for loop. We'll only need to know that we have a collection to operate on. I'm going to create several classes in this single file for the simplicity of…

Contents