C#中foreach与yield的实例详解

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 ;yield break;

登录后复制

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

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

a. 返回类型必须是IEnumerable、IEnumerable、IEnumerator或 IEnumerator。

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 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 GetEnumerator()        {            for (int i = 0; i  Reverse()        {            for (int i = 3; i >= 0; i--)            {                yield return names[i];            }        }        public IEnumerable Subset(int index, int length)        {            for (int i = index; i 

C#中foreach与yield的实例详解

登录后复制

以上就是C#中foreach与yield的实例详解的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2493180.html

(0)
上一篇 2025年3月5日 01:26:58
下一篇 2025年3月5日 01:27:12

AD推荐 黄金广告位招租... 更多推荐

相关推荐

发表回复

登录后才能评论