C#编程-94:迭代器Iterator简单实例
【摘要】
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; namespace IEnumerable...
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace IEnumerableTest
- {
- class Program
- {
- public static IEnumerable GetNextValue()
- {
- yield return "111111";
- yield return "222222";
- yield return "333333";
- yield return "444444";
- yield break;
- yield return "555555";
- }
- static void Main(string[] args)
- {
- Console.WriteLine("===迭代成员===");
- foreach (string item in GetNextValue())
- {
- Console.WriteLine(item);
- }
- Console.WriteLine("===迭代类===");
- Months months = new Months();
- foreach (string item in months)
- {
- Console.WriteLine(item);
- }
- Console.ReadKey();
- }
- }
- class Months : IEnumerable
- {
- string[] months = { "January","February","March","April","May","June","July","August","September","October","November","December"};
- public IEnumerator GetEnumerator()
- {
- for (int i = 0; i < months.Length; i++)
- {
- yield return months[i];
- }
- }
- }
- }
文章来源: pengshiyu.blog.csdn.net,作者:彭世瑜,版权归原作者所有,如需转载,请联系作者。
原文链接:pengshiyu.blog.csdn.net/article/details/81049244
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)