Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unique List<T> in .NET 2

What is a

  • preferably generic;
  • unique(IComparable/IEquitable) valued

collection of objects for .NET 2?

(à la List<T>, or an equivalent of HashSet<T> from .NET 3.5, but without ordered items)

like image 739
serhio Avatar asked Feb 04 '10 14:02

serhio


4 Answers

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.

like image 72
Chris Pitman Avatar answered Nov 11 '22 00:11

Chris Pitman


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
}
like image 32
Ekin Koc Avatar answered Nov 10 '22 22:11

Ekin Koc


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.

like image 2
Frederik Gheysels Avatar answered Nov 10 '22 23:11

Frederik Gheysels


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.

like image 1
Mike Two Avatar answered Nov 11 '22 00:11

Mike Two