2009-05-11 3 views

답변

0

것에 동의 것이라 확신합니다. 유사한 기능을 제공 할 수있는 기능이 있습니다. 당신은 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); 
} 
+0

을. 많은 분들께 감사드립니다. – eusden

0

이를 수행하는 기본 제공 기능이 없습니다. 직접 코딩해야합니다. modulo를 사용하여 동일한 작업을 수행 할 수 있지만 for 루프 (또는 일종의 인덱스)를 사용해야합니다.

var colors = new List { "Red", "Green", "Blue"};

for (int i=0; i < rows.length; i++) { 
    out.write("<p class='" + colours[i % colours.Count] + "'>" + rows[i].Name + "</p>"); 
} 

지금, 나는 당신이 당신은 yield keyword을 사용하여 자신의 롤 수있는 훨씬 더 우아 그 바보 루비 물건 ;-)보다 더