在C#中,要清空HashSet集合,可以使用Clear()方法。以下是一个示例:
using System; using System.Collections.Generic; class Program { static void Main() { HashSetmyHashSet = new HashSet { 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: