C#-foreach与yield

本文详细介绍了C#中的foreach语句及其内部实现原理,并深入探讨了yield语句的不同形式及其在集合迭代中的应用。通过实例展示了如何利用yield return实现灵活的集合遍历。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(转自:http://www.jb51.net/article/34627.htm)

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];
             }
         }
     }
 }

  

 

输出:

 

 
 

转载于:https://www.cnblogs.com/FindSelf/p/3745271.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值