Using Iterator Pattern with C#

Using Iterator Pattern with C#


During software development, navigating data structures and object collections plays a critical role. At this point, the Iterator Pattern provides navigation over a particular collection with a single interface. Using this pattern in C# can make your software architecture more flexible and make your code more readable and easier to maintain. In this article, we will discuss what the Iterator Pattern is, why it is used, and how it is implemented in C#.

What is the Iterator Pattern?

The Iterator Pattern is a design pattern that allows iterating over an object collection. To make a visual analogy, instead of looking for a specific book among a group of books one by one, think of an index that allows you to access all the books by logging in. This pattern provides an interface for accessing the elements in the collection, independent of how the collection is stored. This way, you can use the same interface for different collections.

Implementation of Iterator Pattern in C#

Implementing the iterator pattern in C# is quite simple. First, let's create a collection. Then, we will create an iterator to navigate this collection. Below is a sample code that demonstrates how to create a book collection and how to iterate over it.

Sample Application

using System;
using System.Collections;
using System.Collections.Generic;

// Book object
public class Kitap {
    public string Ad { get; set; }
    public string Yazar { get; set; }
    
    public Kitap(string ad, string yazar) {
        Ad = ad;
        Yazar = yazar;
    }
}

// Book collection
public class KitapKoleksiyonu : IEnumerable<Kitap> {
    private List<Kitap> kitaplar = new List<Kitap>();

    public void Ekle(Kitap kitap) {
        kitaplar.Add(kitap);
    }

    public IEnumerator<Kitap> GetEnumerator() {
        foreach (var kitap in kitaplar) {
            yield return kitap;
        }
    }

    IEnumerator IEnumerable.GetEnumerator() {
        return GetEnumerator();
    }
}

class Program {
    static void Main() {
        KitapKoleksiyonu koleksiyon = new KitapKoleksiyonu();
        koleksiyon.Ekle(new Kitap("Sefiller", "Victor Hugo"));
        koleksiyon.Ekle(new Kitap("1984", "George Orwell"));
        koleksiyon.Ekle(new Kitap("Savaş ve Barış", "Tolstoy"));

        foreach (var kitap in koleksiyon) {
            Console.WriteLine($"{kitap.Ad} - {kitap.Yazar}");
        }
    }
}

In the code above, the Kitap class holds the book information, while the KitapKoleksiyonu class provides an Iterable that stores the books in the collection and allows us to iterate over them. By implementing the IEnumerable interface, it makes it possible to loop over the collection. In the Main method, we add example books and print the collection using a loop.

Conclusion

The Iterator Pattern can be implemented in a simple structure as above in C# and many other programming languages, making your code more organized and readable. Using the same interface for different collection types reduces code duplication and speeds up the development process. The iterator pattern allows you to easily iterate over data structures, even in large and complex projects.