您如何确定两个 HashSet 是否相等(按值,而不是按引用)?

编程入门 行业动态 更新时间:2024-10-27 20:36:29
本文介绍了您如何确定两个 HashSet 是否相等(按值,而不是按引用)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试确定 .NET 3.5 (C#) 中的两个 HashSet 对象是否是相等的集合,即包含相同的值.这似乎是一个显然想要做的事情,但所提供的功能似乎都没有为您提供这些信息.

I am trying to determine if two HashSet objects in .NET 3.5 (C#) are equal sets, i.e. contain the same values. This seems like something one would obviously want to do but none of the provided functions seem to give you this information.

我能想到的方法是检查两个集合的计数是否相等并且一个集合是另一个集合的子集(不正确).我认为可能发生的唯一方法是它们是相等的集合.示例代码:

The way I can think to do this is by checking if the count of the two sets are equal and one set is a subset (not proper) of the other. I think the only way that can happen is if they are equal sets. Example code:

HashSet<int> set1 = new HashSet<int>(); set1.Add(1); set1.Add(2); set1.Add(3); HashSet<int> set2 = new HashSet<int>(); set2.Add(1); set2.Add(2); set2.Add(3); if(set1.Count == set2.Count && set1.IsSubsetOf(set2)) { // do something }

这总是有效吗?有没有更好的办法?为什么HashSet没有public bool IsEqualSetWith()函数?

Would this always work? Is there a better way? Why doesn't HashSet have a public bool IsEqualSetWith() function?

推荐答案

看方法SetEquals.

my_hashset.SetEquals(other);

更多推荐

您如何确定两个 HashSet 是否相等(按值,而不是按引用)?

本文发布于:2023-11-08 13:20:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1569479.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不是   两个   HashSet   按值

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!