Tag Archives: circular

Cinchoo – ChoCircularEnumerator

ChoCircularEnumerator

Cinchoo provides number of helper classes. ChoCircularEnumerator is one of the helper class to iterate over collection items in circular manner. In order to avoid infinite iteration over the collection items, you must provide number of circular iteration while creating enumerator.

How to use

1. Add reference to Cinchoo.Core.dll assembly

2. Namespace Cinchoo.Core.Collections.Generic or Cinchoo.Core.Collections

Using it on Generic Collections

Use Cinchoo.Core.Collection.Generic namespace.

Sample 1: Here we will see how to use it on List<> object, with 2 iterations

static void Main(string[] args)
{
    List<string> symbols = new List<string>();

    for (int x = 0; x < 5; x++)
        symbols.Add(String.Format("ITEM{0}", x));

    int counter = 0;
    foreach (string symbol in new ChoCircularEnumerator<string>(symbols, 2))
    {
        Console.WriteLine("{0}. {1}", ++counter, symbol);
    }
}

When you run the above code, the output will be

1. ITEM0
2. ITEM1
3. ITEM2
4. ITEM3
5. ITEM4
6. ITEM0
7. ITEM1
8. ITEM2
9. ITEM3
10. ITEM4
Press any key to continue . . .

Sample 2: Here we will see how to use it on Dictionary<> object , with 2 iterations

static void Main(string[] args)
{
    Dictionary<int, string> symbols = new Dictionary<int, string>();

    for (int x = 0; x < 5; x++)
        symbols.Add(x, String.Format("VALUE{0}", x));

    foreach (KeyValuePair keyValue in new ChoCircularEnumerator<KeyValuePair<int, string>>(symbols, 2))
    {
        Console.WriteLine("{0}. {1}", keyValue.Key, keyValue.Value);
    }
}

When you run the above code, the output will be

0. ITEM0
1. ITEM1
2. ITEM2
3. ITEM3
4. ITEM4
0. ITEM0
1. ITEM1
2. ITEM2
3. ITEM3
4. ITEM4
Press any key to continue . . .

Using it on Non Generic Collections

Use Cinchoo.Core.Collection namespace.

Sample 3: Here we will see how to use it on ArrayList object, with 2 iterations

static void Main(string[] args)
{
    ArrayList symbols = new ArrayList();

    for (int x = 0; x < 5; x++)
        symbols.Add(String.Format("ITEM{0}", x));

    int counter = 0;
    foreach (object symbol in new ChoCircularEnumerator(symbols, 2))
    {
        Console.WriteLine("{0}. {1}", ++counter, symbol);
    }
}

When you run the above code, the output will be

1. ITEM0
2. ITEM1
3. ITEM2
4. ITEM3
5. ITEM4
6. ITEM0
7. ITEM1
8. ITEM2
9. ITEM3
10. ITEM4
Press any key to continue . . .

Sample 4: Here we will see how to use it on Hashtable object, with 2 iterations

static void Main(string[] args)
{
    Hashtable hash = new Hashtable();

    for (int x = 0; x < 5; x++)
        hash.Add(x, String.Format("VALUE{0}", x));

    foreach (DictionaryEntry dictEntry in new ChoCircularEnumerator(hash, 2))
    {
        Console.WriteLine("{0}. {1}", dictEntry.Key, dictEntry.Value);
    }
}

When you run the above code, the output will be

4. VALUE4
3. VALUE3
2. VALUE2
1. VALUE1
0. VALUE0
4. VALUE4
3. VALUE3
2. VALUE2
1. VALUE1
0. VALUE0
Press any key to continue . . .
Advertisements