.NET 中仅允许唯一项目的集合?
C# 中是否有一个集合不允许您向其中添加重复项?例如,使用
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);
}
}
以下代码的愚蠢类(显然)会抛出异常:
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
会做到这一点,但阅读文档后,类似乎只是一个集合实现(去计算)。
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);
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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
HashSet
就是您要寻找的。来自 MSDN(添加了重点):请注意
HashSet.Add(T item)
方法 如果该项目已添加到集合中,则返回bool
--true
;如果该项目已存在,则false
。HashSet<T>
is what you're looking for. From MSDN (emphasis added):Note that the
HashSet<T>.Add(T item)
method returns abool
--true
if the item was added to the collection;false
if the item was already present.只在 HashSet 上添加一个扩展方法怎么样?
How about just an extension method on HashSet?
从 MSDN 上的
HashSet
页面:(强调我的)
From the
HashSet<T>
page on MSDN:(emphasis mine)
如果您需要的只是确保元素的唯一性,那么 HashSet 就是您所需要的。
当你说“只是一套实施”时,你是什么意思?集合(根据定义)是不保存元素顺序的唯一元素的集合。
If all you need is to ensure uniqueness of elements, then HashSet is what you need.
What do you mean when you say "just a set implementation"? A set is (by definition) a collection of unique elements that doesn't save element order.
只是添加我的 2 美分...
如果您需要一个 ValueExistingException 抛出
HashSet
您还可以轻松创建您的集合:例如,如果您在很多地方需要它,这可能很有用。 。
Just to add my 2 cents...
if you need a ValueExistingException-throwing
HashSet<T>
you can also create your collection easily:this can be useful for example if you need it in many places...
您可以尝试
HashSet
You can try
HashSet<T>
您可以查看某种唯一列表,如下所示
,并且可以像下面这样使用它,
即使在重复项被添加到其中
You may look into something kind of Unique List as follows
and you can use it like follows
will only return
"abc","def","ghi","jkl","mno"
always even when duplicates are added to it作为整体检查的不同方法,这里有 4 种方法来检查集合是否没有任何重复项:
As an overall check different methods here are 4 ways to check if the collection has not any duplicates: