Click or drag to resize

RankedBagT Constructor (IComparerT)

Initializes a new bag instance that uses the supplied comparer.

Namespace:  Kaos.Collections
Assembly:  KaosCollections (in KaosCollections.dll) Version: 4.2.0.0
Syntax
C#
public RankedBag(
	IComparer<T> comparer
)

Parameters

comparer
Type: System.Collections.GenericIComparerT
The comparer to use for sorting items.
Exceptions
ExceptionCondition
InvalidOperationExceptionWhen comparer is null and no other comparer available.
Examples

This program shows using this class as a multiset of case insensitive strings.

C#
using System;
using Kaos.Collections;

namespace ExampleApp
{
    class RbExample01
    {
        static void Main()
        {
            var crayons = new RankedBag<string> (StringComparer.InvariantCultureIgnoreCase)
            { "red", "yellow", "black", "BLACK" };

            crayons.Add ("blue");

            Console.WriteLine ($"There are {crayons.Count} total crayons:");
            foreach (var crayon in crayons)
                Console.WriteLine ($"  {crayon}");

            Console.WriteLine ($"\nThere are {crayons.GetDistinctCount()} distinct colors:");
            foreach (var crayon in crayons.Distinct())
                Console.WriteLine ($"  {crayon}");

            Console.WriteLine ($"\nGot 'gold' crayon? {crayons.Contains ("gold")}");

            // RetainAll respects cardinality so the oldest 'black' is removed:
            crayons.RetainAll (new string[] { "white", "grey", "Black", "red" });

            Console.WriteLine ("\nAfter RetainAll: ");
            foreach (var crayon in crayons)
                Console.WriteLine ($"  {crayon}");
        }

        /* Output:

        There are 5 total crayons:
          black
          BLACK
          blue
          red
          yellow

        There are 4 distinct colors:
          black
          blue
          red
          yellow

        Got 'gold' crayon? False

        After RetainAll:
          BLACK
          red

        */
    }
}
See Also