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]);
// }
//} }
}
IEnumerable_vs_IEnumerator的更多相关文章
随机推荐
- 笔记41 Spring Web Flow——Demo
订购披萨的应用整体比较比较复杂,现拿出其中一个简化版的流程:即用户访问首页,然后输入电话号(假定未注册)后跳转到注册页面,注册完成后跳转到配送区域检查页面,最后再跳转回首页.通过这个简单的Demo用来 ...
- 固定Linux虚拟IP地址
由于我的开发环境是在VMWare虚拟机里安装Centos,然后在host文件中设置拦截,这样就可以直接跳转虚拟机的CentOS,但是虚拟机的IP地址总是会变,就要随时修改host文件,很麻烦.决定虚拟 ...
- php array remove empty values
print_r(array_filter($linksArray)); 參考 Remove empty array elements Remove Empty Array Elements In PH ...
- Oracle使用——PLSQL的中文乱码显示全是问号--Oracle查询中文乱码
问题 这两天刚将PLSQL与Oracle配置好,可是在PLSQL中插入数据时,出现一个问题,PLSQL中的表里无法显示中文,中文无法保存.无法输出,中文在表中显示问号,如图: 原因 经过一番查证, ...
- NOIp2018集训test-9-6(pm)
T1T2是洛谷原题.Orz辉神290,被辉神吊起来打. 题 1 包裹快递 二分答案.这题似乎卡精度,不开long double二分500次都过不去. //Achen #include<algor ...
- NX二次开发-UFUN获取工程图所有视图tag UF_DRAW_ask_views
#include <uf.h> #include <uf_draw.h> #include <uf_drf.h> #include <uf_obj.h> ...
- Tomcat debug 模式, Application一直reload,导致内存溢出
在server.xml配置文件中,将reloable改为false.
- idea 社区版+spring boot+ssm+swagger创建rest api
新手上路,出了好多错,记录一下 1.创建数据库:springBootSsmTest 2.打开IDEA创建 Spring boot项目:File——New——Project——Spring Assist ...
- RN相关命令
添加第三方库 npm install --save xxx react-native link 链接库 react-native react-native -v 查看RN版本 npm info re ...
- 转:CSS盒模型
W3C 组织建议把所有网页上的对像都放在一个盒(box)中,设计师可以通过创建定义来控制这个盒的属性,这些对像包括段落.列表.标题.图片以及层.盒模型主 要定义四个区域:内容(content).边框距 ...