zoukankan      html  css  js  c++  java
  • yield return

    public class DaysOfTheWeek : System.Collections.IEnumerable
    {
        string[] m_Days = { "Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat" };

        public System.Collections.IEnumerator GetEnumerator()
        {
            for (int i = 0; i < m_Days.Length; i++)
            {
                yield return m_Days[i];
            }
        }
    }

    class TestDaysOfTheWeek
    {
        static void Main()
        {
            // Create an instance of the collection class
            DaysOfTheWeek week = new DaysOfTheWeek();

            // Iterate with foreach
            foreach (string day in week)
            {
                System.Console.Write(day + " ");
            }
        }
    }

    ******************************************

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading;

    namespace yeildTST
    {
        
    public class List
        {
            
    public static IEnumerable Power(int number)
            {
                
    while (number< 100)
                {
                    number 
    = number + number;
                    
    yield return number;
                }
            }

            
    static void Main()
            {
                
    foreach (int i in Power(1))
                {
                    Console.Write(
    "{0} ", i);
                }

                Console.ReadLine();
            }
    }

  • 相关阅读:
    最大子数组
    链表插入排序
    链表求和
    有效回文串
    排球比赛计分系统
    超市收银系统
    三层架构
    Wpf+数据库代码封装+策略模式封装
    封装逻辑用策略模式实现
    代码封装
  • 原文地址:https://www.cnblogs.com/mylife_001/p/1964575.html
Copyright © 2011-2022 走看看