All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
解决:操作必须使用一个可更新的查询_[Asp.Net教程]
PHP开发实例:创建PDF中文文档的程序代码_php资料_编程技术
Web基础控件开发--属性(续)_[Asp.Net教程]
用.NET,2.0压缩解压功能处理大型数据
关于textarea的直观换行_JavaScript技术_编程技术
让Perl成为你的嵌入式开发工具_php资料_编程技术
ASP.NET,2.0中判断上传的图片是否符合规格_[Asp.Net教程]
让GridView有新增记录的功能_[Asp.Net教程]
C#,asp.net操作文件_[Asp.Net教程]
PHP实例:精确到每一秒钟的在线人数显示代码_php资料_编程技术
Reading number is top 10 pictures
In the world the most mysterious 21 place landscape3
世界各国15岁的MM有什么不同
Sexy women in 2013--2
Summer is most suitable for young people to travel in China10
自己约的炮,含泪也要打完
西游四格漫画(六)
中国处女图鉴2
大人物的礼物
西游四格漫画(二)
A man's favorite things4
Download software ranking
Proficient in Eclipse
圣殿祭司的ASP.NET.2.0.开发详解-使用C#
1400篇各类破解文章
天龙八部最新服务端
The hero
网页特效实例大全
网络管理员第三版
虚拟机汉化软件
Call Of Duty2
Unix video tutorial4
qq published in(发表于) 2014/7/11 9:21:21 Edit(编辑)
C#教程:C#2.0 新特性 迭代器

C#教程:C#2.0 新特性 迭代器

C#教程:C#2.0 新特性 迭代器

迭代器

迭代器是C# 2.0中的新功能。C# 2.0能在类或结构中支持foreach迭代,而不必实现整个IEnumerable接口。只需提供一个迭代器,即可遍历类中的数据结构。当编译器检测到迭代器时,它将自动生成IEnumerable或IEnumerable接口的Current、MoveNext和Dispose方法。创建了迭代器后,就可以使用foreach对类进行遍历,例如:

本教程来自http://www.isstudy.com

static void Main()

{

ListClass lc = new ListClass();

foreach (int i in lc)

{

System.Console.WriteLine(i);

}

}


创建迭代器最常用的方法是对IEnumerable接口实现GetEnumerator方法,例如:

public System.Collections.IEnumerator GetEnumerator()

{

for (int j = 0;j < max; j++)

{

yield returnj;

}

}


示例

迭代器的实现和使用

下面的示例代码演示了类Year实现迭代器及其使用方法。

public class Year : System.Collections.Ienumerable//实现迭代器的类

{

string[] season = { "Spring", "Summer", "Autumn", "Winter" };

public System.Collections.IEnumerator GetEnumerator()

{

for (int i = 0; i < season.Length; i++)

{

yield return season [i];

}

}

}

class TestClass//使用实现迭代器的类

{

static void Main()

{

Year y= new Year ();

// 使用迭代器

foreach (string s in y)

{

System.Console.Write(s + " ");

}

}

}


输出结果:

Spring Summer Autumn Winter

完整程序代码如下:

★★★★★主程序文件完整程序代码★★★★★

本教程来自http://www.isstudy.com

using System;

using System.Collections.Generic;

using System.Text;

namespace _2_10

{

public class Year : System.Collections.IEnumerable //实现迭代器的类

{

string[] season = { "Spring", "Summer", "Autumn", "Winter" };

public System.Collections.IEnumerator GetEnumerator()

{

for (int i = 0; i < season.Length; i++)

{

yield return season[i];

}

}

}

class TestClass //使用实现迭代器的类

{

static void Main(string[] args)

{

Year y = new Year();

// 使用迭代器

foreach (string s in y)

{

System.Console.Write(s + " ");

}

}

}

}




添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.