• IEnumerable_vs_IEnumerator


    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace IEnumerable_vs_IEnumerator
    {
        class Program
        {
            static void Main(string[] args)
            {
                List<string> WeekDays = new List<string>();
                WeekDays.Add("Sunday");
                WeekDays.Add("Monday");
                WeekDays.Add("Tuesday");
                WeekDays.Add("Wednesday");
                WeekDays.Add("Thursday");
                WeekDays.Add("Friday");
                WeekDays.Add("Saturday");
    
                Console.WriteLine("********** Print Collection with IEnumerable **********");
                IEnumerable<string> iEnum = (IEnumerable<string>)WeekDays;
    
                foreach (string str in iEnum)
                {
                    Console.WriteLine(str);
                }
    
                Console.WriteLine("********** Print Collection with IEnumerator **********");
                IEnumerator<string> iEnumerat = WeekDays.GetEnumerator(); // to convert list into IEnumerator we can invoke the GetEnumerator method
    
                while(iEnumerat.MoveNext())
                {
                    Console.WriteLine(iEnumerat.Current.ToString());
                }
    
                Console.ReadLine();
    
    
    
    
                List<int> myYears = new List<int>();
                myYears.Add(2001);
                myYears.Add(2002);
                myYears.Add(2003);
                myYears.Add(2004);
                myYears.Add(2005);
                myYears.Add(2006);
                myYears.Add(2007);
    
                IEnumerable<int> iEnum2 = (IEnumerable<int>)myYears;
                PrintFirstThreeValues(iEnum2);
                Console.ReadLine();
    
    
    
                IEnumerator<int> iEnumerat2 = myYears.GetEnumerator();
                PrintFirstThreeValues(iEnumerat2);
                Console.ReadLine();
    
            }
    
            static void PrintFirstThreeValues(IEnumerable<int> Obj)
            {
                foreach (int temp in Obj)
                {
                    Console.WriteLine(temp.ToString());
    
                    if(temp>2002)
                    {
                        PrintLastFourValues(Obj);
                    }
                }
            }
    
            static void PrintLastFourValues(IEnumerable<int> Obj)
            {
                foreach (int temp in Obj)
                {
                    Console.WriteLine(temp.ToString());
                }
            }
    
            static void PrintFirstThreeValues(IEnumerator<int> Obj)
            {
                while(Obj.MoveNext())
                {
                    Console.WriteLine(Obj.Current.ToString());
    
                    if ((int)Obj.Current > 2002)
                    {
                        PrintLastFourValues(Obj);
                    }
                }
            }
    
            static void PrintLastFourValues(IEnumerator<int> Obj)
            {
                while(Obj.MoveNext())
                {
                    Console.WriteLine(Obj.Current.ToString());
                }
            }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
            public IEnumerator GetEnumerator()
            {
                // return IEnumerator of our Custom Type
                return (IEnumerator)this;
            }
    
            // IEnumerator interface contains the below three methods Reset, MoveNext, Current
    
            //public void Reset()
            //{
            //    //Get total number of element in a collection
            //    length = slist.Count;
            //    //Setting the pointer to just before the beginning of collection
            //    current = -1;
            //}
    
            //public bool MoveNext()
            //{
            //    //this will increment the counter variable
            //    //and will check whether it is exceeding the actual length of our collection
            //    return (++current < length);
            //}
    
            //public object Current
            //{
            //    get
            //    { //Here "slist" is the collection and "current" is the location pointer 
            //        return (slist[current]);
            //    }
            //}
    
    
        }
    }
    

      

  • 相关阅读:
    2016.5.15——leetcode:Number of 1 Bits ,
    2016.5.14——leetcode-HappyNumber,House Robber
    记录学习过程
    npm 模块安装机制简介
    搭建Vue.js开发环境(window10)
    pwd 显示当前所在的工作路径
    Lucene 6.5.0 入门Demo
    java.lang.UnsupportedClassVersionError
    window.onload 和 $(document).ready(function(){}) 的区别
    plsql + 客户端 连接oracle数据库
  • 原文地址:https://www.cnblogs.com/Jeely/p/11004446.html
Copyright © 2020-2023  润新知