views:

450

answers:

4

What is a (preferably generic) unique(IComparable/IEquitable) valued collection (à la List<T>; ideally a equivalent to HashSet<T> in .NET 3.5 without ordered items) of objects for .NET 2?

+2  A: 

What you need is a Set, as far as I remember there was no Set implementation in 2.0. You can check this out.

Edit: If you really want to implement your own, something like this would do the job in expense of performance on inserts: (I did not test the functionality)

class UniqueList<T> : IList<T>
{
    private IList<T> m_InternalList;

    public UniqueList(IList<T> list)
    {
        m_InternalList = list;
    }

    public System.Collections.ObjectModel.ReadOnlyCollection<T> AsReadOnly()
    {
        return new System.Collections.ObjectModel.ReadOnlyCollection<T>(this);
    }

    #region IList<T> Members

    public int IndexOf(T item)
    {
        return m_InternalList.IndexOf(item);
    }

    public void Insert(int index, T item)
    {
        if (!m_InternalList.Contains(item))
            m_InternalList.Insert(index, item);
    }

    public void RemoveAt(int index)
    {
        m_InternalList.RemoveAt(index);
    }

    public T this[int index]
    {
        get
        {
            return m_InternalList[index];
        }
        set
        {
            if (!m_InternalList.Contains(value))
                m_InternalList[index] = value;
        }
    }

    #endregion

    #region ICollection<T> Members

    public void Add(T item)
    {
        if (!m_InternalList.Contains(item))
            m_InternalList.Add(item);
    }

    public void Clear()
    {
        m_InternalList.Clear();
    }

    public bool Contains(T item)
    {
        return m_InternalList.Contains(item);
    }

    public void CopyTo(T[] array, int arrayIndex)
    {
        m_InternalList.CopyTo(array, arrayIndex);
    }

    public int Count
    {
        get { return m_InternalList.Count; }
    }

    public bool IsReadOnly
    {
        get { return m_InternalList.IsReadOnly; }
    }

    public bool Remove(T item)
    {
        return m_InternalList.Remove(item);
    }

    #endregion

    #region IEnumerable<T> Members

    public IEnumerator<T> GetEnumerator()
    {
        return m_InternalList.GetEnumerator();
    }

    #endregion

    #region IEnumerable Members

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return m_InternalList.GetEnumerator();
    }

    #endregion
}
Ekin Koc
it's a little "hard" solution for me importing a "external" project just for a unique collection. I'd prefer or write a custom collection by myself or a better solution just use a "trick" on the existing framework classes.
serhio
@serhio: I think writing your own collection class is 'harder' then just referencing a 3rd party component that has been proved to work.I think you suffer from the 'not invented here' syndrome. :)
Frederik Gheysels
one method that I really need is `.AsReadOnly()`
serhio
Added AsReadOnly().
Ekin Koc
+1  A: 

We used to use PowerCollections Set class for that in .NET 2. It worked quite well. There was a lot of nice stuff in the library.

Mike Two
+3  A: 

Unfortunately, the first good framework class for this is the HashSet, which you will only get access to with .Net 3.5.

If you are stuck in previous versions., the options are not as nice. The most common is to usea Dictionary type where the Key is the value that you are trying to store. You can wrap this in your own class fairly easily.

If you are willing to go outside the framework all together, there are collections of data structures for .Net, such as NGenerics.

Chris
Thanks for this. I used System.Collections.Generic.Dictionary to get exactly the effect I needed. I wrapped it in a try/catch(ArgumentException)
AJ
+2  A: 

You can use the HashedSet<T> collection that is defined in the Iesi.Collections assembly. This is an open source project, which is also used by NHibernate.

Frederik Gheysels