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#中string类型与json之间相互转换的实现方法

c#线程控制的实例详解

c#中关于async与await的使用详解

c#串口通信的实例教程

c#中如何使用sendmessage?

c#中sql参数传入空值出错误和如何解决办法

c#中实现复制与删除文件的方法

c#中关于匿名委托和lambda表达式的使用详解

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

c# winform跨线程访问控件的图文详解

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




打赏

取消

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

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

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

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

评论

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