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;
}