程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#基礎知識 >> C#:foreach與yield語句的介紹

C#:foreach與yield語句的介紹

編輯:C#基礎知識

1. foreach語句

C#編譯器會把foreach語句轉換為IEnumerable接口的方法和屬性。
代碼如下:

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

foreach語句會解析為下面的代碼段。

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

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

2. yield語句

•yield語句的兩種形式:
代碼如下:

 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塊

代碼如下:

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];
             }
         }
     }
 }

輸出:

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved