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