枚举

在foreach语句中使用枚举,可以迭代集合中的元素,且无需知道集合中的元素个数.

数组或集合实现带GetEumerator()方法的IEumerable接口.GetEumerator()方法返回一个实现IEunmerable接口的枚举.

GetEnumerator()方法用IEnumerable接口定义.foreach语句并不真的需要在集合类中实现这个借口.有一个名为GetEnumerator()的方法,他返回实现了IEnumerator接口的对象就足够了.

IEnumerator接口

foreach语句使用IEnumerator接口的方法和属性,迭代集合中的所有元素.为此IEnumerator定义了Current属性,来返回光标所在的元素,该接口的MoveNext()方法移动到集合的下一个元素上,如果有这个元素,该方法就返回true.如果集合不再有更多的元素,该方法就返回false.

这个借口的泛型版本IEnumerator<T>派生自接口IDisposable,因此定义了Dispose()方法,来清理枚举器占用的资源.

foreach语句

C#的foreach语句不会解释为IL代码中的foreach语句.C#编译器会把foreach语句转换为IEnumerable接口的方法和属性.案例:

int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8 };

foreach (var item in arr)

{

Console.WriteLine(item);

}

很明显,foreach语句很简洁,但是他的有点不仅仅在于此,它的效率也是很高的,不用考虑数组是几维的.案例:

int[,] array = new int[8, 8];

for (int i = 0; i < array.GetLength(0); i++)

{

for (int j = 0; j < array.GetLength(1); j++)

{

Console.WriteLine(array[i,j].ToString());

}

}

Console.ReadKey();

使用foreach:

foreach (int item in array)

{

Console.WriteLine(item.ToString());

}

对于三维或者更多维,foreach语句不用发生任何变化,而对于for语句就要进行修改了.

foreach完成类型转换操作,案例:

int[] array = new int[100];

ArrayList aList = new ArrayList();

aList.AddRange(array);

foreach (int item in aList)

{

Console.WriteLine(item.ToString());

}

for (int i = 0; i < aList.Count; i++)

{

int n = (int)aList[i];

Console.WriteLine(n.ToString());

}

Console.ReadKey();

foreach并没有增加资源使用,由于对于继承了IEnumerable接口的数据类型,才能使用foreach语句,那么对于使用foreach会访问IEnumerable接口中的GetEnumerator()方法来进行枚举,那么对应如上的foreach语句,对应的语句如下:

IEnumerator it = aList.GetEnumerator() as IEnumerator;

using (IDisposable disp = it as IDisposable)

{

while (it.MoveNext())

{

int elem = (int)it.Current;

Console.WriteLine(elem.ToString());

}

}

也即是说再出了foreach语句之后对于IEnumerator的对象也进行IDispose处理.

foreach的两种限制

不能修改枚举成员:

int[] array = new int[100];

foreach (var item in array)

{

item++;//这是错误的,因为枚举成员是只读的

Console.WriteLine(item.ToString());

}

不要对集合进项删除操作:

int[] array = new int[100];

ArrayList alist = new ArrayList();

alist.AddRange(array);

foreach (var item in alist)

{

alist.Remove(item);//这是错误的

Console.WriteLine(item.ToString());

}

对于删除成员和修改成员可以使用for循环来处理,对于一个记录集的多条数据删除问题,也是经常出现的问题,由于在一些记录集中进行删除的时候,在删除操作之后相应的索引也发生了变化,这时候的删除要反过来进行删除:

int[] array = new int[100];

ArrayList alist = new ArrayList();

alist.AddRange(array);

for (int i = alist.Count-1; i >=0; i--)

{

int n = (int)alist[i];

if (n==5)

{

alist.RemoveAt(i);

}

Console.WriteLine(n.ToString());

}

除了上述提到的foreach的两个约束外,foreach可以用于人和循环.

yield语句

C#中的yield语句便于创建枚举器.

yield语句的两种形式:

1.yield return <expression>

2.yield break;

使用一个yield return语句返回集合的一个元素

包含yield语句的方法或属性是迭代器.迭代器必须满足下列要求:

a.返回类型必须是IEnumerable,IEnumerable<T>,IEnumerator或IEnumerator<T>

b.他不能有任何ref或out参数.

yield return语句不能位于try-catch块;yield return语句可以位于try-finally的try中

yield return语句返回集合的一个元素,并移动到下一个元素上.yield break可以停止迭代.

class Program

{

static void Main(string[] args)

{

HelloCollection hello = new HelloCollection();

foreach(string item in hello)

{

Console.WriteLine(item);

}

Console.ReadKey();

}

}

public class HelloCollection

{

public  IEnumerator<string> GetEnumerator()

{

//yield return语句返回集合的一个元素,并移动到下一个元素上

//yield break可以终止迭代

yield return "hello";

yield return "world";

}

}

使用yield return语句实现以不同方式迭代集合的类:

using System;

using System.Collections;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace 枚举

{

class Program

{

static void Main(string[] args)

{

MusicTitles music = new MusicTitles();

foreach(var item in music.GetEnumerator())

{

Console.WriteLine(item);

}

Console.WriteLine();

foreach (string item in music.Reverse())

{

Console.WriteLine(item);

}

Console.WriteLine();

foreach (var item in music.Subset(2,2))

{

Console.WriteLine(item);

}

Console.ReadKey();

}

}

public class MusicTitles

{

string[] names = {"a","b","c","d" };

public IEnumerable<string> GetEnumerator()

{

foreach (string item in names)

{

yield return item;

}

}

public IEnumerable<string> Reverse()

{

for (int i = 3; i >=0; i--)

{

yield return names[i];

}

}

public IEnumerable<string> Subset(int index, int offert)

{

for (int i = index; i < index+offert; i++)

{

yield return names[i];

}

}

}

}

C#编程(三十五)----------foreach和yield的更多相关文章

  1. centos shell脚本编程1 正则 shell脚本结构 read命令 date命令的用法 shell中的逻辑判断 if 判断文件、目录属性 shell数组简单用法 $( ) 和${ } 和$(( )) 与 sh -n sh -x sh -v 第三十五节课

    centos   shell脚本编程1 正则  shell脚本结构  read命令  date命令的用法  shell中的逻辑判断  if 判断文件.目录属性  shell数组简单用法 $( ) 和$ ...

  2. NeHe OpenGL教程 第三十五课:播放AVI

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  3. JAVA之旅(三十五)——完结篇,终于把JAVA写完了,真感概呐!

    JAVA之旅(三十五)--完结篇,终于把JAVA写完了,真感概呐! 这篇博文只是用来水经验的,写这个系列是因为我自己的java本身也不是特别好,所以重温了一下,但是手比较痒于是就写出了这三十多篇博客了 ...

  4. 第三百三十五节,web爬虫讲解2—Scrapy框架爬虫—豆瓣登录与利用打码接口实现自动识别验证码

    第三百三十五节,web爬虫讲解2—Scrapy框架爬虫—豆瓣登录与利用打码接口实现自动识别验证码 打码接口文件 # -*- coding: cp936 -*- import sys import os ...

  5. 孤荷凌寒自学python第三十五天python的文件操作之针对文件操作的os模块的相关内容

     孤荷凌寒自学python第三十五天python的文件操作之针对文件操作的os模块的相关内容 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 一.打开文件后,要务必记得关闭,所以一般的写法应当 ...

  6. 剑指Offer(三十五):数组中的逆序对

    剑指Offer(三十五):数组中的逆序对 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net/bai ...

  7. Java进阶(三十五)java int与integer的区别

    Java进阶(三十五)java int与Integer的区别 前言 int与Integer的区别从大的方面来说就是基本数据类型与其包装类的区别: int 是基本类型,直接存数值,而Integer是对象 ...

  8. Gradle 1.12用户指南翻译——第三十五章. Sonar 插件

    本文由CSDN博客万一博主翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...

  9. SQL注入之Sqli-labs系列第三十四关(基于宽字符逃逸POST注入)和三十五关

    开始挑战第三十四关和第三十五关(Bypass add addslashes) 0x1查看源码 本关是post型的注入漏洞,同样的也是将post过来的内容进行了 ' \ 的处理. if(isset($_ ...

  10. “全栈2019”Java多线程第三十五章:如何获取线程被等待的时间?

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...

随机推荐

  1. 批量初始化数组和memset函数

    对于数组的初始化有一下三种方式: int  a[]={1,2,3,4,5} //通过判断初始化值得个数来却仍数组长度 int b[5]={1,2,3} //数组长度为5,可是初始值却只有三个,因此,不 ...

  2. 关于NOIP2018初赛

    题面 这次PJ初赛有点傻了,可能是因为兴华水土不服吧(在这荒度了六年级的光阴). 选择题 DDDBBAAAABABBBB 第四题 当时懵了,我啥也不知道,于是就开始蒙 A.LAN B.WAN C.MA ...

  3. IdentityServer4使用EFCore生成MySql时的小bug

    EFCore生成PersistedGrantDbContextModelSnapshot的时候发现 b.Property<string>("Data") .IsRequ ...

  4. Springbatch Miscellanea Notes

    1.scope="step",如下图,这是一种后绑定的方式,生成Step的时候,才去创建bean <bean id="testTasklet" paren ...

  5. Vue开源

    Vue开源 - 为移动而生的Vue JS 2组件框架 vonic ★1494 - 快速构建移动端单页应用 eme ★1390 - 优雅的Markdown编辑器 vue-multiselect ★116 ...

  6. react 15

    react 15 最近项目由react0.14.X升级到react 15版本,因为react15还是做了一些相对大一点的更新的(详情可以参考一下我的另一篇文章关于react15的一点总结),相对:来说 ...

  7. 【SPOJ】QTREE6-Query on a tree VI

    题解 老年选手的代码康复计划QAQ 这题又没一遍A,难受 每个节点维护这个节点子树内联通块的大小 维护所有节点轻儿子的\(g[u][0]\)表示所有轻儿子白色的联通块总数 \(g[u][1]\)表示所 ...

  8. 002 jquery基本选择器

    1.选择器 2.基本选择器 3.程序(包含以上五种基本选择器) <!DOCTYPE html> <html> <head> <meta charset=&qu ...

  9. 000 关于IDEA的基本环境配置以及快速使用(git拉载程序,Jdk安装,tomcat部署,应用程序打包运行)

    刚开始工作的时候,不熟悉,所以整理过这个文档. 一:导入git程序 1.准备 git链接 IDEA软件,最好是终极版 2.第一步选择从版本控制上选择git 3.拷贝源于目标地址 4.这时候根据引导进行 ...

  10. Javascript中Object常用方法学习

    1.Object.assign 函数(对象)(JavaScript) 将来自一个或多个源对象中的值复制到一个目标对象.语法: Object.assign(target, ...sources ); 此 ...