温馨提示×

c# hashset怎么删除数据

c#
小樊
95
2024-11-15 22:31:12
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在C#中,要删除HashSet中的数据,可以使用Remove方法。这是一个例子:

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        // 创建一个HashSet并添加一些数据
        HashSet<int> myHashSet = new HashSet<int> { 1, 2, 3, 4, 5 };

        // 输出HashSet中的数据
        Console.WriteLine("HashSet中的数据:");
        foreach (int item in myHashSet)
        {
            Console.WriteLine(item);
        }

        // 删除HashSet中的数据
        int itemToRemove = 3;
        myHashSet.Remove(itemToRemove);

        // 输出删除数据后的HashSet
        Console.WriteLine("\n删除数据后的HashSet:");
        foreach (int item in myHashSet)
        {
            Console.WriteLine(item);
        }
    }
}

在这个例子中,我们首先创建了一个包含一些整数的HashSet。然后,我们使用Remove方法删除了值为3的元素。最后,我们输出删除数据后的HashSet。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:c# set性能如何监控

0