迴圈

int n = 0;
while (n < 5) 
{
    Console.WriteLine(n);
    n++;
}

輸出:

0
1
2
3
4

可以使用 while 迴圈迭代 IEnumerators:

// Call a custom method that takes a count, and returns an IEnumerator for a list
// of strings with the names of theh largest city metro areas.
IEnumerator<string> largestMetroAreas = GetLargestMetroAreas(4);

while (largestMetroAreas.MoveNext())
{
    Console.WriteLine(largestMetroAreas.Current);
}

樣本輸出:

東京/橫濱
紐約地鐵
聖保羅
首爾/仁川