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. [翻译]windows下 连接到 bitnami的phpmyadmin

    bitnami 因为安全考虑,只能 localhost 访问 phpmyadmin 为了能通过SSH 隧道访问 phpMyAdmin,你需要一个ssh 客户端.参考文章介绍使用中选择使用 PuTTY, ...

  2. Java ArrayList使用技巧 - 两个ArrayList去除重复的元素

    方法一.ArrayList中提供的removeAll方法(效率最低) List1.removeAll(mSubList); 方法二.双重循环(比方法一效率高) 双重循环分为内外两层循环,经过测试,将元 ...

  3. js特效玫瑰花

    <script> var b = document.body; var c = document.getElementsByTagName('canvas')[0]; var a = c. ...

  4. 前端必备,十大热门的 JavaScript 框架和库

    JavaScript 框架和库可以说是开源项目中最庞大也是最累的类目了,目前在github 上这一类的项目是最多的,并且几乎每隔一段时间就会出现一个新的项目席卷网络社区,虽然这样推动了创新的发展,但不 ...

  5. Ruby 中文编码

    Ruby 中文编码 前面章节中我们已经学会了如何用 Ruby 输出 "Hello, World!",英文没有问题,但是如果你输出中文字符"你好,世界"就有可能会 ...

  6. jQuery.speech实现文本转语音播报功能

    先放一个实例的地址https://github.com/wenco/speech jQuery.speech是用jQuery写的扩展插件,主要是用来语音播报. 接口调用百度翻译的接口,所以存在url参 ...

  7. eclipse打包插件net.sf.fjep.fatjar

    eclipse打包插件安装 1)将net.sf.fjep.fatjar_0.0.32.jar拷贝到eclipse安装目录中的plugins目录下,然后重启eclipse即可. 软件获取方式: 链接:h ...

  8. [Android开发常见问题-4] RunTime.exec()如何以root权限执行多条指令?

    转:http://blog.csdn.net/alexander_xfl/article/details/9150971 RunTime.exec()这个接口可以说是给我们开发者提供了一个很好的直观操 ...

  9. Apache Shiro RememberMe 1.2.4 反序列化漏洞

    拉取镜像 docker pull medicean/vulapps:s_shiro_1 启动环境 docker run -d -p 80:8080 medicean/vulapps:s_shiro_1 ...

  10. 20140425 malloc和new不同 dynamic何时返回0

    1.malloc/free和new/delete区别 http://blog.csdn.net/hackbuteer1/article/details/6789164 相同点:都可用于申请动态内存和释 ...