枚举

在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. tomcat -> 简介&部署

    Tomcat 简介 Tomcat是Apache软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache.Sun和其他一些公司及个人共同开 ...

  2. 【前端vue开发】vue开发输入姓名,电话,公司表单提交组件

    <template> <div id="parti-info"> <div> <span>您的姓名:</span> &l ...

  3. 解决依赖的moduleBuildConfig.DEBUG总是未false的问题

    Android 开发中一般会通过 BuildConfig.DEBUG 判断是否是 Debug 模式,从而做一些在 Debug 模式才开启的特殊操作,比如打印日志.这样好处是不用在发布前去主动修改,因为 ...

  4. 欢迎来到abc2237512422的博客

    这是第一篇博文. 本博客已迁移到 abc233.site

  5. Java第三阶段学习(八:网络通信协议、UDP与TCP协议)

    一.网络通信协议 1.概念: 通过计算机网络可以使多台计算机实现连接,位于同一个网络中的计算机在进行连接和通信时需要遵守一定的规则,在计算机网络中,这些连接和通信的规则被称为网络通信协议,它对数据的传 ...

  6. word2vec 中的数学原理二 预备知识 霍夫曼树

    主要参考:    word2vec 中的数学原理详解                 自己动手写 word2vec 编码的话,根是不记录在编码中的 这一篇主要讲的就是霍夫曼树(最优二叉树)和编码.  ...

  7. Lambda表达式浅析

    Lambda 表达式是一种可用于创建"委托"或"表达式目录树"类型的"匿名函数".通过使用 lambda 表达式,可以写入可作为参数传递或作 ...

  8. zabbix agent配置文件记录

    一.无论主动还是被动模式都要配置server和linstenPort 二.若要设置主动模式那么要添加ServerActive,若不添加则默认为被动模式

  9. 【*】单线程的redis为什么吞吐量可以这么大

    一.Redis的高并发和快速原因 1.redis是基于内存的,内存的读写速度非常快: 2.redis是单线程的,省去了很多上下文切换线程的时间: 3.redis使用多路复用技术,可以处理并发的连接.非 ...

  10. codevs 5929 亲戚

    5929 亲戚 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold       题目描述 Description 若某个家族人员过于庞大,要判断两个是否是亲戚,确实还很不 ...