C#-foreach與yield

(轉自:http://www.jb51.net/article/34627.htm)express

1. foreach語句數組

C#編譯器會把foreach語句轉換爲IEnumerable接口的方法和屬性。spa

 foreach (Person p in persons)
 {
     Console.WriteLine(p);
 }

foreach語句會解析爲下面的代碼段。.net

 

•調用GetEnumerator()方法,得到數組的一個枚舉
•在while循環中,只要MoveNext()返回true,就一直循環下去
•用Current屬性訪問數組中的元素code

 IEnumerator enumerator = persons. GetEnumerator();
 while (enumerator.MoveNext())
 {
     Person p = (Person) enumerator.Current;
     Console.WriteLine(p);
 }

2. yield語句htm

 

•yield語句的兩種形式: blog

yield return <expression>;
yield break;

  

•使用一個yield return語句返回集合的一個元素
•包含yield語句的方法或屬性是迭代器。迭代器必須知足如下要求
a. 返回類型必須是IEnumerable、IEnumerable<T>、IEnumerator或 IEnumerator<T>。接口

b. 它不能有任何ref或out參數編譯器

•yield return語句不能位於try-catch快。yield return語句能夠位於try-finally的try塊string

try
  {
    // ERROR: Cannot yield a value in the boday of a try block with a catch clause
    yield return "test";
  }
catch
  { }
try   {     //     yield return "test again";   } finally   { } try   { } finally   {     // ERROR: Cannot yield in the body of a finally clause     yield return "";   }

yield break語句能夠位於try塊或catch塊,可是不能位於finally塊

下面的例子是用yield return語句實現一個簡單集合的代碼,以及用foreach語句迭代集合

using System;

 using System.Collections.Generic;

 namespace ConsoleApplication6
 {
     class Program
     {
         static void Main(string[] args)
         {
             HelloCollection helloCollection = new HelloCollection();
             foreach (string s in helloCollection)
             {
                 Console.WriteLine(s);
                 Console.ReadLine();
             }
         }
     }

     public class HelloCollection
     {

         public IEnumerator<String> GetEnumerator()
         {
             // yield return語句返回集合的一個元素,並移動到下一個元素上;yield break能夠中止迭代
             yield return "Hello";
             yield return "World";
         }
     }
 }

  

使用yield return語句實現以不一樣方式迭代集合的類:

 

using System;

 using System.Collections.Generic;

 namespace ConsoleApplication8
 {
     class Program
     {
         static void Main(string[] args)
         {
             MusicTitles titles = new MusicTitles();
             foreach (string title in titles)
             {
                 Console.WriteLine(title);
             }
             Console.WriteLine();

             foreach (string title in titles.Reverse())
             {
                 Console.WriteLine(title);
             }
             Console.WriteLine();

             foreach (string title in titles.Subset(2, 2))
             {
                 Console.WriteLine(title);
                 Console.ReadLine();
             }
         }
     }

     public class MusicTitles
     {
         string[] names = { "a", "b", "c", "d" };
         public IEnumerator<string> GetEnumerator()
         {
             for (int i = 0; i < 4; i++)
             {
                 yield return names[i];
             }
         }

         public IEnumerable<string> Reverse()
         {
             for (int i = 3; i >= 0; i--)
             {
                 yield return names[i];
             }
         }

         public IEnumerable<string> Subset(int index, int length)
         {
             for (int i = index; i < index + length; i++)
             {
                 yield return names[i];
             }
         }
     }
 }

  

 

輸出:

相關文章
相關標籤/搜索