public IEnumerable<string> List1 = new [] { "A", "B", "C" };
public IEnumerable<string> List2 = new [] { "1", "2", "3" };
public IEnumerable<string> List3 = new [] { "Z", "Y" };
public IEnumerator<string> StringEnumerator;
public void InitializeEnumerator()
{
var stringEnumerable = List1.SelectMany(x => List2.SelectMany(y => List3.Select(z => x + y + z)));
StringEnumerator = stringEnumerable.GetEnumerator();
}
public string GetNextString()
{
return StringEnumerator.MoveNext() ? StringEnumerator.Current : null;
}
只需撥打InitializeEnumerator(),那麼你會得到一個新的字符串出每個呼叫的到GetNextString()
InitializeEnumerator();
GetNextString(); //-> A1Z
GetNextString(); //-> A1Y
GetNextString(); //-> A2Z
GetNextString(); //-> A2Y
GetNextString(); //-> A3Z
GetNextString(); //-> A3Y
GetNextString(); //-> B1Z
有它自動初始化並重新初始化它用完之後,像這樣修改GetNextString。
public string GetNextString()
{
if(StringEnumerator == null || !StringEnumerator.MoveNext())
{
InitializeEnumerator();
StringEnumerator.MoveNext();
}
return StringEnumerator.Current;
}
可能你應該嘗試'yield return':[example](http://msdn.microsoft.com/en-us/library/vstudio/9k7k7cf0.aspx) –
就像三個for循環一樣簡單;每個循環中的每個單詞都會打印出結果 – 2013-06-26 15:52:44