C# - Iterator

来源:互联网 发布:python webui 编辑:程序博客网 时间:2024/06/06 17:13
An iterator can be used to step through collections such as lists and arrays.

An iterator method or get accessor performs a custom iteration over a collection. An iterator method uses the yield return statement to return each element one at a time. When ayield return statement is reached, the current location in code is remembered. Execution is restarted from that location the next time the iterator function is called.

You consume an iterator from client code by using a foreach statement or by using a LINQ query.

The return type of an iterator method orget accessor can be IEnumerable, IEnumerable<T>, IEnumerator, or IEnumerator<T>.

You can use yield break statement to end the iteration.

Iterators were introduced in C# in Visual Studio 2005.

4 0
原创粉丝点击