티스토리 뷰


// C# : Iterator

using System;
using System.Collections;
using System.Collections.Generic;

namespace Iteration
{
class Program
{
static void Main(string[] args)
{
List<string> weekDays = new List<string>();
weekDays.Add("Sun");
weekDays.Add("Mon");
weekDays.Add("Tue");
weekDays.Add("Wed");
weekDays.Add("Thu");
weekDays.Add("Fri");
weekDays.Add("Sat");

foreach (string item in weekDays)
{
Console.WriteLine(item);
}

Console.WriteLine("---- End of 1st iteration ----");

IEnumerator myEnumerator = weekDays.GetEnumerator();

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

Console.WriteLine("---- End of 2nd iteration ----");

myEnumerator.Reset(); // No Exception occurs

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

Console.WriteLine("---- End of 3rd iteration ----");
}
}
}

/* Output
Sun
Mon
Tue
Wed
Thu
Fri
Sat
---- End of 1st iteration ----
Sun
Mon
Tue
Wed
Thu
Fri
Sat
---- End of 2nd iteration ----
Sun
Mon
Tue
Wed
Thu
Fri
Sat
---- End of 3rd iteration ----
*/


공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
글 보관함