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();
            }
    }

  • 相关阅读:
    《数据结构》树与二叉树
    C/C++ 一点笔记(1)
    c#中隐藏基类方法的作用
    VS2010 灵活运用快捷操作功能(新手必看)
    C# 之类复制 MemberwiseClone与Clone(深 浅 Clone)
    DLL笔记
    批处理文件
    .NET Remoting(一)
    MSI安装数据库
    关于用户角色权限的一点想法(RBAC)
  • 原文地址:https://www.cnblogs.com/mylife_001/p/1964575.html
Copyright © 2011-2022 走看看