반복기와 양보문 예제 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | using System; namespace EX08_Yield_Enumerator_1 { class MainClass { public static System.Collections.Generic.IEnumerable<int> AddNumber(int n) { int result = 0; for (int i = 0; i <= n; i++) { result += i; yield return result; } } public static void Main (string[] args) { foreach (int i in AddNumber(10)) { Console.WriteLine("{0} ", i); } } } } | cs |
반복기와 양보문 예제 2
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 | using System; namespace EX08_Yield_Enumerator_2 { public class Enemy { public String Name { get; set; } public int HP { get; set; } public int LEVEL { get; set; } } public class EnemyList { public System.Collections.Generic.IEnumerable<Enemy> Next { get { yield return new Enemy { Name = "슬라임", LEVEL = 1, HP = 50 }; yield return new Enemy { Name = "좀비", LEVEL = 2, HP = 100 }; yield return new Enemy { Name = "해골", LEVEL = 2, HP = 150 }; yield return new Enemy { Name = "구울", LEVEL = 3, HP = 220 }; } } } public class MainClass { public static void PrintEnemyList() { var enemys = new EnemyList(); foreach (Enemy enemy in enemys.Next) { Console.WriteLine("LV {0} {1} 의 HP는 {2}", enemy.LEVEL, enemy.Name, enemy.HP); } } static void Main(string[] args) { PrintEnemyList(); } } } | cs |
'Programming > C#' 카테고리의 다른 글
[C# 프로그래밍] 10장 LINQ (0) | 2016.01.11 |
---|---|
[C# 프로그래밍] 9장 델리게이트와 이벤트 (0) | 2016.01.11 |
[C# 프로그래밍] 7장 인터페이스와 추상클래스 (0) | 2016.01.11 |
[C# 프로그래밍] 6강 제네릭 프로그래밍 (0) | 2016.01.11 |
[C# 프로그래밍] 5강 컬렉션 (0) | 2016.01.11 |