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).
You can try
HashSet<T>
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.
From the
HashSet<T>
page on MSDN:(emphasis mine)
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.How about just an extension method on 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...