求高手解释一下程序流程

sunboyqq23 2010-09-11 09:28:04
class App
{
static void Main()
{
GameMoves game = new GameMoves();
IEnumerator enumerator = game.Cross();

while (enumerator.MoveNext())
{
enumerator = (IEnumerator)enumerator.Current;
}

}
}


public class GameMoves
{
private IEnumerator cross;
private IEnumerator circle;

public GameMoves()
{
cross = Cross();
circle = Circle();
}

private int move = 0;

public IEnumerator Cross()
{
while (true)
{
Console.WriteLine("Cross, move {0}", move);
move++;
if (move > 9)
yield break;
yield return circle;
}
}

public IEnumerator Circle()
{
while (true)
{
Console.WriteLine("Circle, move {0}", move);
move++;
if (move > 9)
yield break;
yield return cross;
}
}
}

学C#有段时间了,但是碰到这段程序,完全不能理解,经过调试,发现在执行构造函数的时候,这两个方法Cross(),Circle()根本没有被调用,直到第一次执行enumerator.MoveNext(),控制台才打印出东西。完全不理解整个流程是怎样的,求高人详细的解释一下
...全文
142 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
风尘浪子 2010-09-11
  • 打赏
  • 举报
回复
先看看什么“迭代器”,什么叫做延时执行吧,那你就会明白了。

http://www.ctochina.net/kb/show/328
http://www.sunxin.org/article/578.html
kensouterry 2010-09-11
  • 打赏
  • 举报
回复
IEnumerator is the base interface for all nongeneric enumerators.

For the generic version of this interface see IEnumerator<(Of <(T>)>).

The foreach statement of the C# language (for each in Visual Basic) hides the complexity of the enumerators. Therefore, using foreach is recommended instead of directly manipulating the enumerator.

Enumerators can be used to read the data in the collection, but they cannot be used to modify the underlying collection.

Initially, the enumerator is positioned before the first element in the collection. The Reset method also brings the enumerator back to this position. At this position, calling the Current property throws an exception. Therefore, you must call the MoveNext method to advance the enumerator to the first element of the collection before reading the value of Current.

Current returns the same object until either MoveNext or Reset is called. MoveNext sets Current to the next element.

If MoveNext passes the end of the collection, the enumerator is positioned after the last element in the collection and MoveNext returns false. When the enumerator is at this position, subsequent calls to MoveNext also return false. If the last call to MoveNext returned false, calling Current throws an exception. To set Current to the first element of the collection again, you can call Reset followed by MoveNext.

An enumerator remains valid as long as the collection remains unchanged. If changes are made to the collection, such as adding, modifying, or deleting elements, the enumerator is irrecoverably invalidated and the next call to MoveNext or Reset throws an InvalidOperationException. If the collection is modified between MoveNext and Current, Current returns the element that it is set to, even if the enumerator is already invalidated.

The enumerator does not have exclusive access to the collection; therefore, enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.

貌似只是用到了这接口的一个feature而已,建议多看看MSDN。
wuyq11 2010-09-11
  • 打赏
  • 举报
回复
希望枚举一个可枚举对象(实现IEnumerable接口)的元素,就可以通过IEnumerable接口的GetEnumerator()
方法从可枚举对象请求一个枚举器,可以用枚举器的IEnumerator.MoveNext(),Reset,Current方法遍历。
迭代器 IEnumerable
sunboyqq23 2010-09-11
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 onenewsmile 的回复:]

重点在于理解下面两行:
cross = Cross();
circle = Circle();
并非是执行这两个函数
[/Quote]
能不能详细的解释一下
onenewsmile 2010-09-11
  • 打赏
  • 举报
回复
重点在于理解下面两行:
cross = Cross();
circle = Circle();
并非是执行这两个函数

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧