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

  

最新文章

  1. UML类图的6大关系
  2. vuejs里封装的和IOS,Android通信模块
  3. MySQL5.6 GTID、多线程复制
  4. .net 获取https页面的信息 在iis7.5服务器上不管用
  5. mac平台scala开发环境搭建
  6. android jdbc 远程数据库
  7. 查看javadoc
  8. jquery 之validate 笔记
  9. 【单峰函数,三分搜索算法(Ternary_Search)】UVa 1476 - Error Curves
  10. http://phantomjs.org/page-automation.html
  11. 1354 - IP Checking(水题)
  12. jQuery形式可以计算,它包含了无线电的变化价格,select价格变化,删除行动态计算加盟
  13. Python进阶-继承中的MRO与super
  14. 看完给跪了:技术大牛总结的Github与华为软件开发云完整对比
  15. 爬 NationalData ,虽然可以直接下,但还是爬一下吧
  16. clean-room 洁净室软件工程
  17. WCF访问超时:HTTP 请求已超过xx:yy分配的超时。为此操作分配的时间可能是较长超时的一部分。
  18. tyvj 创世纪 - 基环树
  19. ASP.NET Web API 框架研究 Web Host模式下的消息处理管道
  20. [转发]如何在ASP.NET的web.config配置文件中添加MIME类型

热门文章

  1. file_get_contents(): SSL operation failed with code 1
  2. 自定义Java annotation
  3. delphi JPG转为BMP存入数据库
  4. delphi Treeview用法
  5. Django -- 分页 -- 批量导入
  6. bzoj1006题解
  7. Django使用步骤
  8. docker快速安装kibana
  9. ionic-CSS:ionic 按钮
  10. Ubuntu下安装fcitx+搜狗输入法