唯一列表在.NET 2中

发布于 2024-08-20 02:32:53 字数 237 浏览 6 评论 0原文

什么是

  • 最好的通用;
  • .NET 2唯一(IComparable/IEquitable) 值

对象集合?

à la List,或 .NET 3.5 中的 HashSet 的等效项,但没有有序项)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

围归者 2024-08-27 02:32:54

您可以使用 Iesi 中定义的 HashedSet 集合.Collections 组装。
这是一个开源项目,NHibernate 也使用它。

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.

深空失忆 2024-08-27 02:32:54

我们曾经在 .NET 2 中使用 PowerCollections Set 类。它运行得很好。图书馆里有很多好东西。

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.

王权女流氓 2024-08-27 02:32:53

不幸的是,第一个好的框架类是 HashSet,您可以使用它只能通过 .Net 3.5 访问。

如果您被困在以前的版本中,那么选项就不那么好了。最常见的是使用字典类型,其中键是您要存储的值。您可以相当轻松地将其包装在您自己的类中。

如果您愿意完全脱离框架,可以使用 .Net 的数据结构集合,例如 N泛型

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.

清音悠歌 2024-08-27 02:32:53

你需要的是一个 Set,据我记得 2.0 中没有 Set 实现。您可以查看

编辑:如果你真的想实现自己的,类似这样的事情会以牺牲插入性能为代价来完成这项工作:(我没有测试该功能)

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
}

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文