在C#中,可以使用foreach循環來遍歷HashSet集合。以下是一個示例:
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// 創建一個HashSet集合
HashSet<int> myHashSet = new HashSet<int> { 1, 2, 3, 4, 5 };
// 使用foreach循環遍歷HashSet集合
foreach (int item in myHashSet)
{
Console.WriteLine(item);
}
}
}
在這個示例中,我們首先創建了一個包含整數的HashSet集合myHashSet
。然后,我們使用foreach循環遍歷集合中的每個元素,并將其輸出到控制臺。