国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#:foreach與yield語句的介紹

2020-01-24 03:33:38
字體:
供稿:網(wǎng)友

1. foreach語句

C#編譯器會把foreach語句轉(zhuǎn)換為IEnumerable接口的方法和屬性。

復(fù)制代碼 代碼如下:

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

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

•調(diào)用GetEnumerator()方法,獲得數(shù)組的一個(gè)枚舉
•在while循環(huán)中,只要MoveNext()返回true,就一直循環(huán)下去
•用Current屬性訪問數(shù)組中的元素

復(fù)制代碼 代碼如下:

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

2. yield語句

•yield語句的兩種形式:

復(fù)制代碼 代碼如下:

 yield return <expression>;
 yield break;
 

•使用一個(gè)yield return語句返回集合的一個(gè)元素
•包含yield語句的方法或?qū)傩允堑鳌5鞅仨殱M足以下要求
a. 返回類型必須是IEnumerable、IEnumerable<T>、IEnumerator或 IEnumerator<T>。

b. 它不能有任何ref或out參數(shù)

•yield return語句不能位于try-catch快。yield return語句可以位于try-finally的try塊

復(fù)制代碼 代碼如下:

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語句實(shí)現(xiàn)一個(gè)簡單集合的代碼,以及用foreach語句迭代集合

復(fù)制代碼 代碼如下:

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語句返回集合的一個(gè)元素,并移動到下一個(gè)元素上;yield break可以停止迭代
             yield return "Hello";
             yield return "World";
         }
     }
 }

使用yield return語句實(shí)現(xiàn)以不同方式迭代集合的類:

復(fù)制代碼 代碼如下:

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

輸出:

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 永德县| 石棉县| 大田县| 桑植县| 剑川县| 大冶市| 怀仁县| 宁津县| 山西省| 青岛市| 新竹市| 乌拉特中旗| 砀山县| 集贤县| 辰溪县| 读书| 勃利县| 靖远县| 新营市| 永嘉县| 阳曲县| 黄石市| 沐川县| 庆阳市| 日照市| 遂平县| 府谷县| 麻城市| 苗栗县| 平果县| 棋牌| 长沙县| 凤山县| 钟祥市| 白朗县| 喀喇沁旗| 广灵县| 竹溪县| 乌恰县| 香格里拉县| 罗山县|