zoukankan      html  css  js  c++  java
  • 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]);
            //    }
            //}
    
    
        }
    }
    

      

  • 相关阅读:
    什么是超参数
    KNN算法(K近邻算法)实现与剖析
    pandas中na_values与keep_default_na
    一篇文章搞懂python2、3编码
    深度学习基础篇之逻辑回归拟合二维数据
    采集万方医药方向的期刊+文章+作者信息(数据量千万级)
    win10安装tensorflow (cpu版)
    内存文件的读写
    海康威视面试python后端题
    Scrapy 采集需要登录注册的网站
  • 原文地址:https://www.cnblogs.com/Jeely/p/11004446.html
Copyright © 2011-2022 走看看