C#中foreach与yield的实例详解


当前第2页 返回上一页

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

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#开发中遇到的问题分享

泛型的概述和具体使用

c#中关于逆变和协变的详解

c#接口的问题的解决办法详解

c#开发微信门户及应用(二)之微信消息处理和应答的图文代码教程

c#中关于infinity与nan的简单介绍

c#如何使用ilgenerator实现动态生成函数的实例

c#中五种访问修饰符作用范围实例详解

c#如何实现自动更新本地程序的实例分析

c#中datetime与时间戳转换的实例代码

更多相关阅读请进入《foreach》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...