■ IEnumerable 인터페이스를 사용하는 방법을 보여준다.
▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Employee[] employeeArray = new Employee[3] { new Employee("John", "Smith" ), new Employee("Jim" , "Johnson"), new Employee("Sue" , "Rabon" ) }; EmployeeEnumerable employeeEnumerable = new EmployeeEnumerable(employeeArray); foreach(Employee employee in employeeEnumerable) { Console.WriteLine(employee.FirstName + " " + employee.LastName); } |
▶ Employee.cs
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
/// <summary> /// 직원 /// </summary> public class Employee { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 이름 - FirstName /// <summary> /// 이름 /// </summary> public string FirstName { get; set; } #endregion #region 성 - LastName /// <summary> /// 성 /// </summary> public string LastName { get; set; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - Employee(firstName, lastName) /// <summary> /// 생성자 /// </summary> /// <param name="firstName">이름</param> /// <param name="lastName">성</param> public Employee(string firstName, string lastName) { FirstName = firstName; LastName = lastName; } #endregion } |
▶ EmployeeEnumerator.cs
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
using System; using System.Collections; /// <summary> /// 직원 열거자 /// </summary> public class EmployeeEnumerator : IEnumerator { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 직원 배열 /// </summary> private Employee[] employeeArray; /// <summary> /// 현재 위치 /// </summary> private int currentPosition = -1; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 직원 배열 - EmployeeArray /// <summary> /// 직원 배열 /// </summary> public Employee[] EmployeeArray { get { return this.employeeArray; } set { this.employeeArray = value; } } #endregion #region 현재 항목 - IEnumerator.Current /// <summary> /// 현재 항목 /// </summary> object IEnumerator.Current { get { return Current; } } #endregion #region 현재 항목 - Current /// <summary> /// 현재 항목 /// </summary> public Employee Current { get { try { return this.employeeArray[this.currentPosition]; } catch(IndexOutOfRangeException) { throw new InvalidOperationException(); } } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - EmployeeEnumerator(employeeArray) /// <summary> /// 생성자 /// </summary> /// <param name="employeeArray">직원 배열</param> public EmployeeEnumerator(Employee[] employeeArray) { this.employeeArray = employeeArray; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 다음 요소로 이동하기 - MoveNext() /// <summary> /// 다음 요소로 이동하기 /// </summary> /// <returns>현재 위치가 마지막 요소 뒤 위치 여부</returns> public bool MoveNext() { this.currentPosition++; return (this.currentPosition < this.employeeArray.Length); } #endregion #region 위치 처음 요소 앞으로 이동하기 - Reset() /// <summary> /// 위치 처음 요소 앞으로 이동하기 /// </summary> public void Reset() { this.currentPosition = -1; } #endregion } |
▶ EmployeeEnumerable.cs
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
using System; using System.Collections; /// <summary> /// 직원 열거가능 /// </summary> public class EmployeeEnumerable : IEnumerable { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 직원 배열 /// </summary> private Employee[] employeeArray; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - EmployeeEnumerable(employeeArray) /// <summary> /// 생성자 /// </summary> /// <param name="employeeArray">직원 배열</param> public EmployeeEnumerable(Employee[] employeeArray) { this.employeeArray = new Employee[employeeArray.Length]; for(int i = 0; i < employeeArray.Length; i++) { this.employeeArray[i] = employeeArray[i]; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 열거자 구하기 - IEnumerable.GetEnumerator() /// <summary> /// 열거자 구하기 /// </summary> /// <returns>열거자</returns> IEnumerator IEnumerable.GetEnumerator() { return (IEnumerator)GetEnumerator(); } #endregion #region 열거자 구하기 - GetEnumerator() /// <summary> /// 열거자 구하기 /// </summary> /// <returns>직원 열거자</returns> public EmployeeEnumerator GetEnumerator() { return new EmployeeEnumerator(this.employeeArray); } #endregion } |