IEnumerable_vs_IEnumerator
vs IEnumerable
2023-09-27 14:23:17 时间
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]); // } //} } }
相关文章
- 【VS开发】 Windows平台下管道的使用
- 【VS开发】【miscellaneous】windows(64位)下使用curl命令
- 【Qt开发】【VS开发】VS2010+Qt开发环境搭建
- 【VS开发】字符串进制等转换关系及方法
- 【VS开发】千兆以太网的传输速度
- 【VS开发】MP4与H.264
- 【VS开发】VC下加载JPG/GIF/PNG图片的两种方法
- 伪数据科学家 VS 真数据科学家
- vs启动报4.X的错
- MyBatis vs Hibernate,到底哪个性能更好?
- Angular 复习与进阶系列 – Component 组件 の Angular Component vs Custom Elements
- crucible VS gerrit
- Facebook 默认开发环境采用 VS Code
- VS(Visual Studio)中Nuget的使用
- Windows 7 使用 VS.NET 2003 时遇到“LNK1201: 写入程序数据库时出错”的解决方案
- 去掉VS中的警告错误:warning C4819
- VS_Qt 常见错误(持续更新)