C#中的foreach和yield
来源:互联网 发布:阿里云ecs windows教程 编辑:程序博客网 时间:2024/05/16 07:21
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]; } } }}
以上动图由“图斗罗”提供
阅读全文
0 0
- C#中的foreach和yield
- C#高级编程三十五天----foreach和yield
- foreach()中的yield的使用
- C# 和 Java 中的 foreach
- C#中的yield关键字
- C#中的yield return
- C#中的yield关键字
- c#中的yield关键字
- C#中的yield关键字
- C#中的yield return
- C# 中的"yield"使用
- 了解 C# foreach 内部语句和使用 yield 实现的自定义迭代器
- C#中的yield return 和return有什么区别
- C#中的using和yield return混合使用
- C# yield return 和 yield break
- 有关C#中的foreach
- C#中的foreach遍历
- C#中的foreach
- 利用python进行数据分析译者给出的注意事项
- c# 图片浏览器
- POJ 1131 Octal Fractions——高精度
- C++template(模板)的使用
- PMCAFF微课堂 (已结束)| 京东平台产品负责人:如何打造一个支撑3200万日订单量的平台型产品
- C#中的foreach和yield
- 从《芈月传》看热门IP在互联网视频行业的“前世今生”
- JFinal中java如何处理数据库查询出的重复数据
- python问题解决----把\xe6\xa8\xa1\这种字符转为普通汉字?
- 关于bmp 4字节对齐
- 乐观锁导致并发失败解决方案
- 盘点史上最无聊的6款APP,脑洞大开我已无力吐槽
- Nginx实现高并发优化
- 添加管理微信公众号素材-微信公众号使用教程7