国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 迁西县| 锦屏县| 开封市| 伊通| 泽库县| 顺平县| 桦川县| 财经| 霍林郭勒市| 丰镇市| 南部县| 嘉义县| 蕉岭县| 沙洋县| 双鸭山市| 枞阳县| 湘西| 蒙山县| 克什克腾旗| 汨罗市| 油尖旺区| 利川市| 陇川县| 调兵山市| 黎平县| 防城港市| 元氏县| 曲沃县| 新闻| 石泉县| 志丹县| 石渠县| 寿光市| 柘荣县| 兴化市| 北碚区| 河池市| 龙川县| 武城县| 株洲市| 嫩江县|