C#:foreach與yield語句的介紹

來源:互聯網
上載者:User

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

輸出:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.