C#编程-94:迭代器Iterator简单实例_彭世瑜_新浪博客
2024-06-12 09:30:16 阅读次数:22
c++,leetcode
- 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];
- }
- }
- }
- }
版权声明:本文内容来自第三方投稿或授权转载,原文地址:https://blog.51cto.com/mouday/3045191,作者:彭世瑜,版权归原作者所有。本网站转在其作品的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如因作品内容、版权等问题需要同本网站联系,请发邮件至ctyunbbs@chinatelecom.cn沟通。
上一篇:Vue课程27-开发模式下配置sourceMap
下一篇:C语言反汇编 - 流程控制与循环结构