벌써 C#이 버전 3.0까지 나왔다는데, 달봉이는 아직도 2.0 버전에 당황을 하곤 한다.
지금 소개할 문법도 오늘 아침 처음으로 본 것이다.
using System;
using System.Collections.Generic;
class Test
{
public static void Main()
{
foreach (string s in GetItems())
Console.WriteLine(s);
}
private static IEnumerable GetItems()
{
yield return "Hello yield 1";
yield return "Hello yield 2";
yield return "Hello yield 3";
yield return "Hello yield 4";
yield return "Hello yield 5";
}
}
"yield return" 문이 있는데, iteration의 다음 값을 반환한다고 한다.
"yield break" 문도 있단다.
『The yield return statement produces the next value of the iteration.
The yield break statement indicates that the iteration is complete.』
GetItems() 메소드의 리턴 타입이 IEnumerable 이라는 것도 주목할 부분이다.
참조 문서
c# 2.0 iterators -
http://community.bartdesmet.net/blogs/bart/archive/2006/07/06/4121.aspx
'개발 > C#' 카테고리의 다른 글
| 이벤트 핸들링 패턴 (0) | 2009/04/23 |
|---|---|
| C# 2.0 iterators (0) | 2009/04/23 |
| LINQ 시리즈 11 - LINQ 마구잡이 마무리 (0) | 2009/04/23 |
| LINQ 시리즈 10 - 쿼리 표현식(Query expression) 이해 (0) | 2009/04/23 |
| LINQ 시리즈 09 - type inference(타입 유추) 좀 더 (0) | 2009/04/23 |
| LINQ 시리즈 08 - 메소드 확장( Extension Methods) (0) | 2009/04/23 |
