Cinchoo – ChoReverseEnumerator

ChoReverseEnumerator

This is one another custom enumerator that Cinchoo framework provides. ChoReverseEnumerator is a custom enumerator iterates over collection items in reverse order.

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

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 ChoReverseEnumerator<string>(symbols))
    {
        Console.WriteLine("{0}. {1}", ++counter, symbol);
    }
}

When you run the above code, the output will be

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

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

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<int, string> keyValue in new ChoReverseEnumerator<KeyValuePair<int, string>>(symbols))
    {
        Console.WriteLine("{0}. {1}", keyValue.Key, keyValue.Value);
    }
}

When you run the above code, the output will be

4. VALUE4
3. VALUE3
2. VALUE2
1. VALUE1
0. VALUE0
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 < 5; x++)
        symbols.Add(String.Format("ZITEM{0}", x));

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

When you run the above code, the output will be

1. ZITEM4
2. ZITEM3
3. ZITEM2
4. ZITEM1
5. ZITEM0
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 < 5; x++)
        hash.Add(x, String.Format("VALUE{0}", x));

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

When you run the above code, the output will be

0. VALUE0
1. VALUE1
2. VALUE2
3. VALUE3
4. VALUE4
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