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]);
// }
//} }
}

  

IEnumerable_vs_IEnumerator的更多相关文章

随机推荐

  1. BIO、NIO和AIO

    BIO(Blocking I/O)同步阻塞I/O 这是最基本与简单的I/O操作方式,其根本特性是做完一件事再去做另一件事,一件事一定要等前一件事做完,这很符合程序员传统的顺序来开发思想,因此BIO模型 ...

  2. Vultr账号被锁定的几个常见原因

    很多小伙伴使用都在问Vultr账号为什么又是会出现被锁定的情况,今天我们就来了解一下Vultr账号被锁定的几个常见原因. 1.Vultr主机违规使用 按照Vultr主机商的要求,我们的Vultr主机也 ...

  3. Tools: windbg 使用指南

    windbg使用 符号表C:\Symbols; SRV*C:\Symbols*http://msdl.microsoft.com/download/symbols 系统变量_NT_SYMBOL_PAT ...

  4. leetcood学习笔记-171-excel表列序号

    题目描述: 方法: class Solution: def titleToNumber(self, s: str) -> int: num = 0 r = 1 for i in s[::-1]: ...

  5. R语言 判断

    R语言判断 决策结构要求程序员指定要由程序评估或测试的一个或多个条件,以及如果条件被确定为真则要执行的一个或多个语句,如果条件为假则执行其他语句. 以下是在大多数编程语言中的典型决策结构的一般形式 R ...

  6. 转-pycharm建立项目

    转自:https://blog.csdn.net/m0_37544464/article/details/79171913 本文针对环境已经配置好的Pycharm建立新项目 1.第一步 2.第二步 在 ...

  7. scrapy爬取cnblogs文章列表

    scrapy爬取cnblogs文章 目标任务 安装爬虫 创建爬虫 编写 items.py 编写 spiders/cnblogs.py 编写 pipelines.py 编写 settings.py 运行 ...

  8. Spring Boot 文件下载

    1. 文件下载类 import javax.servlet.http.HttpServletResponse; import java.io.*; public class DownloadUtil ...

  9. Jupyter的使用复习

    Jupyter的使用 esc+m 切换到markdown模式 shift+enter 运行 a 向上新增代码块 b 向下新增代码块 y python代码模式 file-->download as ...

  10. POJ 3667 线段树区间合并裸题

    题意:给一个n和m,表示n个房间,m次操作,操作类型有2种,一种把求连续未租出的房间数有d个的最小的最左边的房间号,另一个操作时把从x到x+d-1的房间号收回. 建立线段树,值为1表示未租出,0为租出 ...