C#中foreach与yield的示例分析

这篇文章将为大家详细讲解有关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 <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];
            }
        }
    }
}

C#中foreach与yield的示例分析

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

关于“C#中foreach与yield的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

网站标题:C#中foreach与yield的示例分析
链接地址:https://www.cdcxhl.com/article28/jgscjp.html

成都网站建设公司_创新互联,为您提供网站建设网站内链App开发网站设计公司用户体验网站营销

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

h5响应式网站建设