标签:item unicode put for height app important boa round
GetEnumerator()方法的实质实现:
1 publicclass List 2 { 3 public static IEnumerable Power(int number(2), int exponent(8))//使用yield return 使得返回值是一个迭代器类型 4 { 5 int counter =0; 6 int result =1; 7 while (counter++< exponent) 8 { 9 result = result * number; 10 yield return result; 11 } 12 } 13 14 staticvoid Main() 15 { 16 foreach (int i in Power(2, 8)) 17 { 18 Console.Write("{0} ", i); 19 } 20 } 21 }
/*
输出: Output: 2 4 8 16 32 64 128 256 // */
了解枚举的机制后,就不得不讲道yield:
1 class Program 2 { 3 staticvoid Main(string[] args) 4 { 5 foreach (var item in Person.Run()) 6 { 7 Console.WriteLine(item); 8 } 9 } 10 } 11 class Person 12 { 13 public static IEnumerable<int> Run() 14 { 15 List<int> list = new List<int>(); 16 foreach (var item in list) 17 { 18 yieldreturn item; 19 } 20 } 21 }
//yield会给你生成一个枚举类,而这个枚举类和刚才List中的Enumerator枚举类又无比的一样
标签:item unicode put for height app important boa round
原文地址:http://www.cnblogs.com/xiaoliangge/p/6006055.html