Design Pattern Examples
Overview of object-oriented design patterns
Iterator_Exercise.cs
Go to the documentation of this file.
1
5
6using System;
7using System.Collections.Generic;
8using System.Linq;
9using System.Text;
10using System.Threading.Tasks;
11
13{
23 internal class Iterator_Exercise
24 {
28 // ! [Using Iterator in C#]
29 public void Run()
30 {
31 Console.WriteLine();
32 Console.WriteLine("Iterator Exercise");
33
34 // For this example, the class already has built into it the data
35 // to be iterated over.
37
38 Console.WriteLine(" Iterating over keys only:");
39 var keyIterator = items.GetKeys();
40 for (string? item = keyIterator.Next(); item != null; item = keyIterator.Next())
41 {
42 Console.WriteLine(" {0}", item);
43 }
44
45 Console.WriteLine(" Iterating over values only:");
46 var valueIterator = items.GetValues();
47 for (string? item = valueIterator.Next(); item != null; item = valueIterator.Next())
48 {
49 Console.WriteLine(" {0}", item);
50 }
51
52 Console.WriteLine(" Iterating over all items:");
53 var itemIterator = items.GetItems();
54 for (ItemPair? item = itemIterator.Next(); item != null; item = itemIterator.Next())
55 {
56 Console.WriteLine(" {0} = {1}", item.Key, item.Value);
57 }
58
59 Console.WriteLine(" Done.");
60 }
61 // ! [Using Iterator in C#]
62 }
63}
Represents a key/value pair where the key and value are strings.
Example of using the Iterator Pattern in C#.
void Run()
Executes the example for the Iterator Pattern in C#.
Represents a container that offers up two kinds of iterators for the hardcoded contents.
IIterator< string > GetValues()
Retrieve an iterator over the "value" part of the data, where the data returned from the iterator is ...
IIterator< string > GetKeys()
Retrieve an iterator over the "key" part of the data, where the data returned from the iterator is a ...
IIterator< ItemPair > GetItems()
Retrieve an iterator over the data that returns an ItemPair object containing both key and value for ...
The namespace containing all Design Pattern Examples implemented in C#.