• <small id='nfTj1'></small><noframes id='nfTj1'>

        <bdo id='nfTj1'></bdo><ul id='nfTj1'></ul>
      1. <tfoot id='nfTj1'></tfoot>

        <legend id='nfTj1'><style id='nfTj1'><dir id='nfTj1'><q id='nfTj1'></q></dir></style></legend>

        <i id='nfTj1'><tr id='nfTj1'><dt id='nfTj1'><q id='nfTj1'><span id='nfTj1'><b id='nfTj1'><form id='nfTj1'><ins id='nfTj1'></ins><ul id='nfTj1'></ul><sub id='nfTj1'></sub></form><legend id='nfTj1'></legend><bdo id='nfTj1'><pre id='nfTj1'><center id='nfTj1'></center></pre></bdo></b><th id='nfTj1'></th></span></q></dt></tr></i><div id='nfTj1'><tfoot id='nfTj1'></tfoot><dl id='nfTj1'><fieldset id='nfTj1'></fieldset></dl></div>
      2. 仅允许 .NET 中唯一项目的集合?

        时间:2023-10-25
      3. <tfoot id='YwTGs'></tfoot>
        <legend id='YwTGs'><style id='YwTGs'><dir id='YwTGs'><q id='YwTGs'></q></dir></style></legend>

          <small id='YwTGs'></small><noframes id='YwTGs'>

                <i id='YwTGs'><tr id='YwTGs'><dt id='YwTGs'><q id='YwTGs'><span id='YwTGs'><b id='YwTGs'><form id='YwTGs'><ins id='YwTGs'></ins><ul id='YwTGs'></ul><sub id='YwTGs'></sub></form><legend id='YwTGs'></legend><bdo id='YwTGs'><pre id='YwTGs'><center id='YwTGs'></center></pre></bdo></b><th id='YwTGs'></th></span></q></dt></tr></i><div id='YwTGs'><tfoot id='YwTGs'></tfoot><dl id='YwTGs'><fieldset id='YwTGs'></fieldset></dl></div>
                  <tbody id='YwTGs'></tbody>
                  <bdo id='YwTGs'></bdo><ul id='YwTGs'></ul>

                  本文介绍了仅允许 .NET 中唯一项目的集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  C# 中是否存在不允许您向其中添加重复项的集合?例如,用愚蠢的类

                  Is there a collection in C# that will not let you add duplicate items to it? For example, with the silly class of

                  public class Customer {
                      public string FirstName { get; set; }
                      public string LastName { get; set; }
                      public string Address { get; set; }
                  
                      public override int GetHashCode() {
                          return (FirstName + LastName + Address).GetHashCode();
                      }
                  
                      public override bool Equals(object obj) {
                          Customer C = obj as Customer;
                          return C != null && String.Equals(this.FirstName, C.FirstName) && String.Equals(this.LastName, C.LastName) && String.Equals(this.Address, C.Address);
                      }
                  }
                  

                  以下代码将(显然)抛出异常:

                  The following code will (obviously) throw an exception:

                  Customer Adam = new Customer { Address = "A", FirstName = "Adam", LastName = "" };
                  Customer AdamDup = new Customer { Address = "A", FirstName = "Adam", LastName = "" };
                  
                  Dictionary<Customer, bool> CustomerHash = new Dictionary<Customer, bool>();
                  CustomerHash.Add(Adam, true);
                  CustomerHash.Add(AdamDup, true);
                  

                  但是有没有一个类可以同样保证唯一性,但没有 KeyValuePairs?我认为 HashSet<T> 会这样做,但是在阅读了文档后,似乎该类只是一个集合实现(go figure).

                  But is there a class that will similarly guarantee uniqueness, but without KeyValuePairs? I thought HashSet<T> would do that, but having read the docs it seems that class is just a set implementation (go figure).

                  推荐答案

                  HashSet<T> 正是您要找的.来自 MSDN(已添加重点):

                  HashSet<T> is what you're looking for. From MSDN (emphasis added):

                  HashSet<T> 类提供高性能的集合操作.集合是不包含重复元素且其元素没有特定顺序的集合.

                  The HashSet<T> class provides high-performance set operations. A set is a collection that contains no duplicate elements, and whose elements are in no particular order.

                  请注意 HashSet.Add(T item) 方法 返回一个 bool -- true 如果项目被添加到集合中;false 如果项目已经存在.

                  Note that the HashSet<T>.Add(T item) method returns a bool -- true if the item was added to the collection; false if the item was already present.

                  这篇关于仅允许 .NET 中唯一项目的集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何找到集合的所有分区 下一篇:如何使用 LINQ 从一组数字中查找 n 项的所有组合?

                  相关文章

                    <bdo id='UCpnK'></bdo><ul id='UCpnK'></ul>
                  <i id='UCpnK'><tr id='UCpnK'><dt id='UCpnK'><q id='UCpnK'><span id='UCpnK'><b id='UCpnK'><form id='UCpnK'><ins id='UCpnK'></ins><ul id='UCpnK'></ul><sub id='UCpnK'></sub></form><legend id='UCpnK'></legend><bdo id='UCpnK'><pre id='UCpnK'><center id='UCpnK'></center></pre></bdo></b><th id='UCpnK'></th></span></q></dt></tr></i><div id='UCpnK'><tfoot id='UCpnK'></tfoot><dl id='UCpnK'><fieldset id='UCpnK'></fieldset></dl></div>

                  1. <tfoot id='UCpnK'></tfoot>

                    <small id='UCpnK'></small><noframes id='UCpnK'>

                    <legend id='UCpnK'><style id='UCpnK'><dir id='UCpnK'><q id='UCpnK'></q></dir></style></legend>