C#:foreach與yield語句的介紹
1. foreach語句
C#編譯器會(huì)把foreach語句轉(zhuǎn)換為IEnumerable接口的方法和屬性。
foreach (Person p in persons)
{
Console.WriteLine(p);
}
foreach語句會(huì)解析為下面的代碼段。
•調(diào)用GetEnumerator()方法,獲得數(shù)組的一個(gè)枚舉
•在while循環(huán)中,只要MoveNext()返回true,就一直循環(huán)下去
•用Current屬性訪問數(shù)組中的元素
IEnumerator enumerator = persons. GetEnumerator();
while (enumerator.MoveNext())
{
Person p = (Person) enumerator.Current;
Console.WriteLine(p);
}
2. yield語句
•yield語句的兩種形式:
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塊
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è)簡(jiǎn)單集合的代碼,以及用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語句返回集合的一個(gè)元素,并移動(dòng)到下一個(gè)元素上;yield break可以停止迭代
yield return "Hello";
yield return "World";
}
}
}
使用yield return語句實(shí)現(xiàn)以不同方式迭代集合的類:
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#實(shí)現(xiàn)進(jìn)制轉(zhuǎn)換
這篇文章介紹了C#實(shí)現(xiàn)進(jìn)制轉(zhuǎn)換的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
C# 未將對(duì)象引用設(shè)置到對(duì)象的實(shí)例
c#開發(fā)過程中出現(xiàn)未將對(duì)象引用設(shè)置到對(duì)象的實(shí)例,錯(cuò)誤一般是下面的原因,軟件中也是因?yàn)闆]有獲取到數(shù)據(jù)導(dǎo)致,需要的朋友可以參考下2022-09-09
C#?使用?Filestream?修改大文件指定位置數(shù)據(jù)
這篇文章主要介紹了C#?使用?Filestream修改大文件指定位置數(shù)據(jù),文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09
C#程序最小化到托盤圖標(biāo)操作步驟與實(shí)現(xiàn)代碼
設(shè)置窗體屬性showinTask=false;加notifyicon控件notifyIcon1,為控件notifyIcon1的屬性Icon添加一個(gè)icon圖標(biāo);添加窗體最小化事件(首先需要添加事件引用)接下來介紹實(shí)現(xiàn)代碼,感興趣的朋友可以研究下2012-12-12

