ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Enumerator 예제
    .NET 2009. 4. 28. 10:53
    반응형

    http://personar95.springnote.com/pages/2959548



    Enumerator 예제

    Created : 2009/03/23

     

    가장 간단한 Enumerator 예제다.

    1.    11         static void Main(string[] args)

         12         {

         13             int index = 0;

         14

         15             string[] obj = { "빨", "주", "노", "초", "파", "남", "보" };

         16

         17             IEnumerator e = obj.GetEnumerator(); // IEnumerator 가져오기

         18

         19             while (e.MoveNext())  // 첫번째 부터 7번째 요소까지

         20             {

         21                 Console.Write(" - {0}", e.Current);

         22             }

         23             Console.WriteLine();

         24             e.Reset();

         25             while (e.MoveNext())

         26             {

         27                 if ((index % 2) == 1)

         28                 {

         29                     Console.Write(" - {0}", e.Current);

         30                 }

         31                 index++;

         32             }

         33             Console.WriteLine(Environment.NewLine);

         34         }

     

    Enumerable, Enumerator 고급 예제다. 어렵지만 나중에 또 볼 것

    1.     9     public class Person

         10     {

         11         public Person(string fName, string lName)

         12         {

         13             this.firstName = fName;

         14             this.lastName = lName;

         15         }

         16 

         17         public string firstName;

         18         public string lastName;

         19     }

         20 

         21     public class People : IEnumerable

         22     {

         23         private Person[] _people;

         24         public People(Person[] pArray)

         25         {

         26             _people = new Person[pArray.Length];

         27 

         28             for (int i = 0; i < pArray.Length; i++)

         29             {

         30                 _people[i] = pArray[i];

         31             }

         32         }

         33 

         34         public IEnumerator GetEnumerator()

         35         {

         36             return new PeopleEnum(_people);

         37         }

         38     }

         39 

         40     public class PeopleEnum : IEnumerator

         41     {

         42         public Person[] _people;

         43 

         44         // Enumerators are positioned before the first element

         45         // until the first MoveNext() call.

         46         int position = -1;

         47 

         48         public PeopleEnum(Person[] list)

         49         {

         50             _people = list;

         51         }

         52 

         53         public bool MoveNext()

         54         {

         55             position++;

         56             return (position < _people.Length);

         57         }

         58 

         59         public void Reset()

         60         {

         61             position = -1;

         62         }

         63 

         64         public object Current

         65         {

         66             get

         67             {

         68                 try

         69                 {

         70                     return _people[position];

         71                 }

         72                 catch (IndexOutOfRangeException)

         73                 {

         74                     throw new InvalidOperationException();

         75                 }

         76             }

         77         }

         78     }

         79 

         80     class Program

         81     {

         82         static void Main(string[] args)

         83         {

         84             Person[] peopleArray = new Person[3]

         85             {

         86                 new Person("John", "Smith"),

         87                 new Person("Jim", "Johnson"),

         88                 new Person("Sue", "Rabon"),

         89             };

         90 

         91             People peopleList = new People(peopleArray);

         92             foreach (Person p in peopleList)

         93                 Console.WriteLine(p.firstName + " " + p.lastName);

         94         }

         95     }

     

    페이지 히스토리

    2009-04-14 10:07 에 페르소나95님이 마지막으로 수정

    댓글 (0)

      로그인한 사람에게만 글쓰기를 허용하고 있습니다. 로그인 해주세요.

      반응형

      댓글

    Designed by Tistory.