温馨提示×

c# set集合 如何清空

c#
小樊
82
2024-12-04 04:25:41
栏目: 编程语言

在C#中,要清空HashSet集合,可以使用Clear()方法。以下是一个示例:

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        HashSet<int> myHashSet = new HashSet<int> { 1, 2, 3, 4, 5 };
        Console.WriteLine("HashSet before clearing: " + string.Join(", ", myHashSet));

        myHashSet.Clear();
        Console.WriteLine("HashSet after clearing: " + string.Join(", ", myHashSet));
    }
}

输出结果:

HashSet before clearing: 1, 2, 3, 4, 5
HashSet after clearing:

0