Cinchoo – ChoSteppedEnumerator

ChoSteppedEnumerator

This is one another custom enumerator that Cinchoo framework provides. ChoSteppedEnumerator is a custom enumerator iterates over collection items while skipping some items.

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 steps

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

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

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

When you run the above code, the output will be

1. ITEM0
2. ITEM3
3. ITEM6
Press any key to continue . . .

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

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

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

    foreach (KeyValuePair<int, string> keyValue in new ChoSteppedEnumerator<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
3. ITEM3
6. ITEM6
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 steps

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

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

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

When you run the above code, the output will be

1. ITEM0
2. ITEM3
3. ITEM6
Press any key to continue . . .

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

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

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

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

When you run the above code, the output will be

6. VALUE6
3. VALUE3
0. VALUE0
Press any key to continue . . .
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s