A simple Set collection

In a Microsoft internal discussion, someone asked for Set functionality in a collection.  I threw this together. 

What do you think?  Useful or no?

    using System.Collections.Generic;

    using System.Diagnostics;

    class Set<TItem>

    {

        Dictionary<TItem, bool> _list = new Dictionary<TItem, bool>();

        public bool Contains(TItem item) { return _list.ContainsKey(item); }

        public void Add(TItem item)

        {

            if (!Contains(item))

            {

                this._list.Add(item, false);

            }

        }

        public void Remove(TItem item)

        {

            if (!Contains(item))

            {

     // TODO: pick a better exception type

                throw new System.Exception();

            }

            this._list.Remove(item);

        }

        public int Count { get { return this._list.Count; } }

    }

    class Test

    {

        static void Main(string[] args)

        {

            Set<int> set = new Set<int>();

            Debug.Assert(set.Count == 0);

            Debug.Assert(set.Contains(1) == false);

            set.Add(1);

        Debug.Assert(set.Count == 1);

            Debug.Assert(set.Contains(1) == true);

            Debug.Assert(set.Contains(2) == false);

            set.Add(1);

            Debug.Assert(set.Count == 1);

            Debug.Assert(set.Contains(1) == true);

            Debug.Assert(set.Contains(2) == false);

            set.Add(2);

            Debug.Assert(set.Count == 2);

            Debug.Assert(set.Contains(1) == true);

            Debug.Assert(set.Contains(2) == true);

            set.Remove(1);

            Debug.Assert(set.Count == 1);

            Debug.Assert(set.Contains(1) == false);

            Debug.Assert(set.Contains(2) == true);

        }

    }

Comments

  • Anonymous
    May 26, 2005
    google power collections, that's useful. This is interesting.

  • Anonymous
    May 26, 2005
    Built-in support for sets would be useful, but only if there were Set operations to go along with it (which is where Power Collections has provided value). Perhaps adding set operations to the framework, useful on ArrayList, List<T>, etc would be the more direct approach.

    This would preserve multiple copies of an item in a List, but you could then provide a specialization of List with more traditional Set logic on that count, as you've written here.

    Since we're on the topic of nice things to have, baking the Singleton pattern into .NET would be another useful idea, I think.

    public static interface ISingleton<T> where T: new
    {
    public static T Instance
    {
    get;
    }
    }

    ... or an actual generic static class to handle singleton instantiation (delegating a singleton constructor).

  • Anonymous
    May 26, 2005
    I agree with Keith. Baking in a formal Singleton pattern would have been/be a useful addition to the Framework.

    As was pointed out already, for Set operations I'd direct the internal discussion to the Power Collections site:

    http://www.wintellect.com/powercollections/

    As far as I can tell it is considered a quasi-official extention to the Framework.

  • Anonymous
    June 05, 2005
    Very good. Great staff.

  • Anonymous
    May 26, 2009
    PingBack from http://castironbakeware.info/story.php?title=jaybaz-ms-weblog-a-simple-set-collection

  • Anonymous
    May 31, 2009
    PingBack from http://indoorgrillsrecipes.info/story.php?id=285