Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我们有SetEquals方法来检查不同的集合类型是否相等。但是将对元素执行什么相等测试呢?将Equals或被ReferenceEquals使用?
SetEquals
Equals
ReferenceEquals
在API中,它只是声明:
判断当前集合和指定集合是否包含相同的元素
它是“集合用来考虑元素相等性的那个”,即用于Addand的那个Contains。例如:
Add
Contains
var set = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "abc", "def" }; var list = new List<string> { "ABC", "DEF" }; Console.WriteLine(set.SetEquals(list)); // True