public class PowersOf2 { static void Main() { // Display powers of 2 up to the exponent of 8: , )) { Console.Write("{0} ", i); } } public static System.Collections.IEnumerable Power(int number, int exponent) { ; ; i < exponent; i++) { result…
浅谈yield http://www.cnblogs.com/qlb5626267/archive/2009/05/08/1452517.html .NET中yield关键字的用法 http://blog.csdn.net/aspnet2002web/article/details/6083417 When you use the yield keyword in a statement, you indicate that the method, operator, or get access…
一般来说当我们创建自定义集合的时候为了让其能支持foreach遍历,就只能让其实现IEnumerable接口(可能还要实现IEnumerator接口) 但是我们也可以通过使用yield关键字构建的迭代器方法来实现foreach的遍历,且自定义的集合不用实现IEnumerable接口 注:虽然不用实现IEnumerable接口 ,但是迭代器的方法必须命名为GetEnumerator() ,返回值也必须是IEnumerator类型 实例代码以及简单说明如下: class Person { publi…
using System; using System.Collections.Generic; using System.Reflection; using System.Text.RegularExpressions; using System.Linq; namespace Test { //C#的yield关键字由来以久,如果我没有记错的话,应该是在C# 2.0中被引入的. //相信大家此关键字的用法已经了然于胸,很多人也了解yield背后的“延迟赋值”机制. //但是即使你知道这个机制,…