것에 동의 것이라 확신합니다. 유사한 기능을 제공 할 수있는 기능이 있습니다. 당신은 IEnumerable 인터페이스를 사용하기 위해이 네임 스페이스를 포함해야합니다 :
public static void Main()
{
string[] myColors = { "red", "green", "blue" };
// this would be your external loop, such as the one building up the table in the RoR example
for (int index = 0; index < 3; index++)
{
foreach (string color in Cycle(myColors))
{
Console.WriteLine("Current color: {0}", color);
}
}
}
public static IEnumerable Cycle<T>(T[] items)
{
foreach (T item in items)
{
yield return item;
}
}
주기 방법은 코드 샘플에 generics를 사용하여 위의 기타의 사용을 허용하기 : 여기
using System.Collections;
하는 예입니다 유형.
int[] myInts = { 0, 1, 2, 3 };
bool[] myBools = { true, false };
을 그리고 루프에서 당신은 할 수 있습니다 : 예를 들어, myColors 선언 어디 사용할 수있는 최고의
foreach (int i in Cycle(myInts))
{
Console.WriteLine("Current int: {0}", i);
}
foreach (bool b in Cycle(myBools))
{
Console.WriteLine("Current bool: {0}", b);
}
을. 많은 분들께 감사드립니다. – eusden