c# set集合 如何删除元素

在C#中,要从HashSet集合中删除一个元素,可以使用Remove方法。这是一个示例:using System;using System.Collections.Generic;class Program{static void Main(){HashSet mySet = new HashSet { 1, 2, 3, 4, 5 };Console.WriteLi

在C#中,要从HashSet<T>集合中删除一个元素,可以使用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<int>集合。然后,我们使用Remove方法删除元素3。最后,我们遍历集合并输出结果。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,请发送邮件至 55@qq.com 举报,一经查实,本站将立刻删除。转转请注明出处:https://www.szhjjp.com/n/1204497.html

(0)
派派
上一篇 2024-12-04
下一篇 2024-12-04

发表回复

登录后才能评论