欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#:foreach與yield語句的介紹

 更新時間:2013年03月08日 14:40:08   作者:  
C#:foreach與yield語句的介紹,需要的朋友可以參考一下

1. foreach語句

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

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

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

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

•調(diào)用GetEnumerator()方法,獲得數(shù)組的一個枚舉
•在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;
 

•使用一個yield return語句返回集合的一個元素
•包含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語句實現(xiàn)一個簡單集合的代碼,以及用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語句返回集合的一個元素,并移動到下一個元素上;yield break可以停止迭代
             yield return "Hello";
             yield return "World";
         }
     }
 }

使用yield return語句實現(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];
             }
         }
     }
 }

輸出:

相關(guān)文章

  • C#多線程之Semaphore用法詳解

    C#多線程之Semaphore用法詳解

    這篇文章主要為大家詳細(xì)介紹了C#多線程之Semaphore用法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • C#打印PDF文檔的10種方法(小結(jié))

    C#打印PDF文檔的10種方法(小結(jié))

    這篇文章主要介紹了C#打印PDF文檔的10種方法(小結(jié)),可分多種情況來進(jìn)行,如設(shè)置靜默打印、指定打印頁碼范圍和打印紙張大小、雙面打印、黑白打印等等,一共分成10種等,感興趣的可以了解一下
    2019-04-04
  • C#計算字符串相似性的方法

    C#計算字符串相似性的方法

    這篇文章主要介紹了C#計算字符串相似性的方法,實例分析了C#計算字符串相似性的原理與算法實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-07-07
  • 淺析C# Dynamic關(guān)鍵字

    淺析C# Dynamic關(guān)鍵字

    這篇文章主要介紹了C# Dynamic關(guān)鍵字的相關(guān)資料,文中講解非常細(xì)致,對大家學(xué)習(xí)C# Dynamic關(guān)鍵字有所幫助,感興趣的朋友可以了解下
    2020-08-08
  • C#將dll打包到程序中的具體實現(xiàn)

    C#將dll打包到程序中的具體實現(xiàn)

    這篇文章介紹了C#將dll打包到程序中的具體實現(xiàn),有需要的朋友可以參考一下
    2013-10-10
  • C# 二進(jìn)制數(shù)組與結(jié)構(gòu)體的互轉(zhuǎn)方法

    C# 二進(jìn)制數(shù)組與結(jié)構(gòu)體的互轉(zhuǎn)方法

    本文將和大家介紹 MemoryMarshal 輔助類,通過這個輔助類用來實現(xiàn)結(jié)構(gòu)體數(shù)組和二進(jìn)制數(shù)組的相互轉(zhuǎn)換,對C# 二進(jìn)制數(shù)組與結(jié)構(gòu)體的互轉(zhuǎn)方法感興趣的朋友一起看看吧
    2023-09-09
  • C#集合之自定義集合類

    C#集合之自定義集合類

    這篇文章介紹了C#集合之自定義集合類,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-05-05
  • 詳解如何獲取C#類中發(fā)生數(shù)據(jù)變化的屬性信息

    詳解如何獲取C#類中發(fā)生數(shù)據(jù)變化的屬性信息

    這篇文章主要介紹了詳解如何獲取C#類中發(fā)生數(shù)據(jù)變化的屬性信息,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • C#語句先后順序?qū)Τ绦虻慕Y(jié)果有影響嗎

    C#語句先后順序?qū)Τ绦虻慕Y(jié)果有影響嗎

    有朋友問我,C#中C#語句先后順序影響程序的結(jié)果嗎?告訴大家,答案是肯定的,絕對影響程序的結(jié)果,所以在程序中一定要注意C#語句的順序
    2015-10-10
  • npoi2.0將datatable對象轉(zhuǎn)換為excel2007示例

    npoi2.0將datatable對象轉(zhuǎn)換為excel2007示例

    這篇文章主要介紹了npoi2.0將datatable對象轉(zhuǎn)換為excel2007示例的相關(guān)資料
    2014-04-04

最新評論