Collection that allows only unique items in .NET?

后端 未结 7 2483
天涯浪人
天涯浪人 2020-12-13 16:35

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 F         


        
7条回答
  •  情书的邮戳
    2020-12-13 17:20

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

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

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

提交回复
热议问题