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的更多相关文章
随机推荐
- Vuejs input 和 textarea 元素中使用 v-model 实现双向数据绑定
demo <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <titl ...
- vue for 循环例子 2
demo <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf- ...
- h5 app 设置全屏
h5 app的全屏和沉浸式状态栏是不一样的 全屏模式 常见使用场景:如果页面是全屏游戏,一般会直接让状态栏消失,也就是页面全屏.webview高度全屏了,状态栏没有了.写法: 终端支持:没有终端类型限 ...
- LoadRunner模拟REST接口的json请求
LoadRunner模拟REST接口的json请求 现在很多手机应用的性能测试,REST接口调用通过json格式,在用loadrunner模拟这些json请求时,需要开发提供 1.供接口地址 2.提交 ...
- jQuery 遍历 - eq() 方法
<!DOCTYPE html> <html> <head> <style> div { width:60px; height:60px; margin: ...
- paper 15 :整理的CV代码合集
这篇blog,原来是西弗吉利亚大学的Li xin整理的,CV代码相当的全,不知道要经过多长时间的积累才会有这么丰富的资源,在此谢谢LI Xin .我现在分享给大家,希望可以共同进步!还有,我需要说一下 ...
- 基于Netty的RPC架构学习笔记(二):netty服务器
文章目录 简介 Netty服务端Hello World案例 举个
- jQuery 基本使用
index.html <head><meta http-equiv="Content-Type" content="text/html; charset ...
- HDU1501-Zipper-字符串的dfs
Given three strings, you are to determine whether the third string can be formed by combining the ch ...
- Area in Triangle /// oj10229
题目大意: 给出三角形的三个顶点 再给一条绳(绳长不超过三角形周长) 求绳子在三角形中能围出的最大面积 题解链接 http://blog.sina.com.cn/s/blog_6a46cc3f0100 ...