阅读量:3
在C#中,要对HashSet进行并集操作,可以使用UnionWith()方法。这是一个例子:
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
HashSet<int> set1 = new HashSet<int> { 1, 2, 3, 4, 5 };
HashSet<int> set2 = new HashSet<int> { 4, 5, 6, 7, 8 };
// 对两个HashSet进行并集操作
set1.UnionWith(set2);
// 输出并集后的HashSet
Console.WriteLine("并集后的HashSet:");
foreach (int item in set1)
{
Console.WriteLine(item);
}
}
}
在这个例子中,我们创建了两个HashSet(set1和set2),然后使用UnionWith()方法将它们合并。最后,我们遍历并输出合并后的HashSet。
以上就是关于“c# set集合如何进行并集操作”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm