开发者

Unique List<T> in .NET 2

开发者 https://www.devze.com 2022-12-18 22:32 出处:网络
What is a preferably generic; unique(IComparable/IEquitable) valued collectio开发者_运维百科n of objects for .NET 2?

What is a

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

collectio开发者_运维百科n of objects for .NET 2?

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


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.


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
}


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号