Cinchoo – ChoConstraintEnumerator

ChoConstraintEnumerator

This is one another custom enumerator that Cinchoo framework provides. ChoConstraintEnumerator is a custom enumerator iterates over collection items matches the passed constraint logic.

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, matches items having value 3 or 5

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 ChoConstraintEnumerator<string>(symbols, (x) => x.EndsWith("3") || x.EndsWith("5")))
    {
        Console.WriteLine("{0}. {1}", ++counter, symbol);
    }
}

When you run the above code, the output will be

1. ITEM3
2. ITEM5
Press any key to continue . . .

Sample 2: Here we will see how to use it on Dictionary<> object, matches items having key either 3 or 5

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

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

    foreach (KeyValuePair<int, string> keyValue in new ChoConstraintEnumerator<KeyValuePair<int, string>>(dict, (x) => x.Key == 3 || x.Key == 5))
    {
        Console.WriteLine("{0}. {1}", keyValue.Key, keyValue.Value);
    }
}

When you run the above code, the output will be

3. VALUE3
5. VALUE5
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, matches items having value 3 or 5

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

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

    int counter1 = 0;
    foreach (object symbol in new ChoConstraintEnumerator(symbols1, (x) => x.ToString().EndsWith("3") || x.ToString().EndsWith("5")))
    {
        Console.WriteLine("{0}. {1}", ++counter1, symbol);
    }
}

When you run the above code, the output will be

1. ZITEM3
2. ZITEM5
Press any key to continue . . .

Sample 4: Here we will see how to use it on Hashtable object, matches items having key either 3 or 5

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 ChoConstraintEnumerator(hash, (x) => ((DictionaryEntry)x).Key.ToString() == "3" || ((DictionaryEntry)x).Key.ToString() == "5"))
    {
        Console.WriteLine("{0}. {1}", dictEntry.Key, dictEntry.Value);
    }
}

When you run the above code, the output will be

5. VALUE5
3. VALUE3
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