代码分析:
(1).注意第17行,MyColl<T>的构造函数不能写成MyColl<T>...
(2).24的IEnumerable<T> GetEnumerator()来自IEnumberable<T>, 而29行的IEnumerable.GetEnumerator()来自IEnumberable。
在泛型时候,调用第24行。但是为什么要写29行呢?因为IEnumberable<T>本身继承自 IEnumberable,即
public interface IEnumerable<T> : IEnumerable
{
IEnumerator<T> GetEnumerator();
}
public interface IEnumerable
{
IEnumerator GetEnumerator();
}
(3).第26行的NestEnum是新建的一个类,继承自IEnumerator<T>。在类NestEnum中。
T Current{get;}继承自IEnumerator<T>
object Current { get; } bool MoveNext(); void Reset();继承自IEnumerator
void Dispose()继承自IDisposable
如下
public interface IEnumerator<T> : IDisposable, IEnumerator
(4).调用顺序是:一旦获得枚举器,它首先调用MoveNext,然后使用Current的返回值初始化变量n,如此开始循环。如果循环没有其他的出口路径,循环将持续下去直到MoveNext返回false。这是枚举器结束遍历,然后调用Dispose函数。reset一直没有被调用,我猜想可能是应该在类内部处理特殊情况的
[csharp] view plaincopy
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using System.Reflection;
using System.Threading;
using System.Drawing;
using System.Collections.Specialized;
namespace CSharpConsole
{
public class MyColl<T>:IEnumerable<T>
{
public List<T> arr = new List<T>();
public MyColl(T t1,T t2,T t3,T t4)
{
arr.Add(t1);
arr.Add(t2);
arr.Add(t3);
arr.Add(t4);
}
public IEnumerator<T> GetEnumerator()//这个函数来自IEnumerable<T>
{
return new NestEnum<T>(this);
}
IEnumerator IEnumerable.GetEnumerator()//这个函数来自IEnumerable
{
return GetEnumerator();
}
}
public class NestEnum<T> : IEnumerator<T>
{
public NestEnum(MyColl<T> coll)
{
this.coll = coll;
this.index = -1;
Console.WriteLine("CON");
}
public T Current//这个函数来自IEnumerator<T>
{
get
{
Console.WriteLine("CURRENT T");
return current;
}
}
public void Dispose()//这个函数来自IDisposable
{
Console.WriteLine("DISPOSE");
}
object IEnumerator.Current//这个函数来自IEnumerator
{
get
{
Console.WriteLine("CURENT IENUMBLE");
return Current;
}
}
public bool MoveNext()//这个函数来自IEnumerator
{
Console.WriteLine("MOVE");
index++;
if (index == coll.arr.Count)
return false;
else
{
current = coll.arr[index];
return true;
}
}
public void Reset()//这个函数来自IEnumerator
{
Console.WriteLine("RESET");
current = default(T);
index = 0;
}
private MyColl<T> coll;
private int index;
private T current;
}
public class Program
{
public static void Fun()
{
MyColl<int> col = new MyColl<int>(1, 2, 3, 4);
foreach (int i in col)
{
}
}
static void Main()
{
Fun();
Console.WriteLine("-----------------------------------------");
MyColl<int> col=new MyColl<int>(1,2,3,4);
foreach (int i in col)
{
}
Console.WriteLine("-----------------------------------------");
foreach (int i in col)
{
}
}
}
}
[csharp] view plaincopy
</pre><pre name="code" class="csharp">
[csharp] view plaincopy
</pre><pre name="code" class="csharp">
[csharp] view plaincopy
</pre><pre name="code" class="csharp">
如果用yield return的话,就变的简单多了,只要修改一个函数如下;而且NestEnum的类都不需要了
[csharp] view plaincopy
public IEnumerator<T> GetEnumerator()//这个函数来自IEnumerable<T>
{
for (int i = 2; i < arr.Count; i++)
yield return arr[i];
for (int i = 0; i < 2; i++)
yield return arr[i];
}
[csharp] view plaincopy
</pre><p>yield return还有一种不放在类里的用法,很奇怪,注意函数返回值只能是IEnumerable<T>或者是IEnumerable 不能使IEnumerator<T>和IEnumerator,因为需要返回值里面有</p><p>GetEnumerator()方法</p><p><pre name="code" class="csharp"> public class Program
{
static void Main()
{
List<int> intlist = new List<int>();
intlist.Add(1);
intlist.Add(2);
intlist.Add(3);
intlist.Add(4);
foreach(int n in CreateReverseIterator(intlist))
{
Console.WriteLine(n);
}
}
static IEnumerable<T> CreateReverseIterator<T>(IList<T> list)
{
int count = list.Count;
for (int i = count - 1; i >= 0; i--)
{
yield return list[i];
}
}
}
(1).注意第17行,MyColl<T>的构造函数不能写成MyColl<T>...
(2).24的IEnumerable<T> GetEnumerator()来自IEnumberable<T>, 而29行的IEnumerable.GetEnumerator()来自IEnumberable。
在泛型时候,调用第24行。但是为什么要写29行呢?因为IEnumberable<T>本身继承自 IEnumberable,即
public interface IEnumerable<T> : IEnumerable
{
IEnumerator<T> GetEnumerator();
}
public interface IEnumerable
{
IEnumerator GetEnumerator();
}
(3).第26行的NestEnum是新建的一个类,继承自IEnumerator<T>。在类NestEnum中。
T Current{get;}继承自IEnumerator<T>
object Current { get; } bool MoveNext(); void Reset();继承自IEnumerator
void Dispose()继承自IDisposable
如下
public interface IEnumerator<T> : IDisposable, IEnumerator
(4).调用顺序是:一旦获得枚举器,它首先调用MoveNext,然后使用Current的返回值初始化变量n,如此开始循环。如果循环没有其他的出口路径,循环将持续下去直到MoveNext返回false。这是枚举器结束遍历,然后调用Dispose函数。reset一直没有被调用,我猜想可能是应该在类内部处理特殊情况的
[csharp] view plaincopy
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using System.Reflection;
using System.Threading;
using System.Drawing;
using System.Collections.Specialized;
namespace CSharpConsole
{
public class MyColl<T>:IEnumerable<T>
{
public List<T> arr = new List<T>();
public MyColl(T t1,T t2,T t3,T t4)
{
arr.Add(t1);
arr.Add(t2);
arr.Add(t3);
arr.Add(t4);
}
public IEnumerator<T> GetEnumerator()//这个函数来自IEnumerable<T>
{
return new NestEnum<T>(this);
}
IEnumerator IEnumerable.GetEnumerator()//这个函数来自IEnumerable
{
return GetEnumerator();
}
}
public class NestEnum<T> : IEnumerator<T>
{
public NestEnum(MyColl<T> coll)
{
this.coll = coll;
this.index = -1;
Console.WriteLine("CON");
}
public T Current//这个函数来自IEnumerator<T>
{
get
{
Console.WriteLine("CURRENT T");
return current;
}
}
public void Dispose()//这个函数来自IDisposable
{
Console.WriteLine("DISPOSE");
}
object IEnumerator.Current//这个函数来自IEnumerator
{
get
{
Console.WriteLine("CURENT IENUMBLE");
return Current;
}
}
public bool MoveNext()//这个函数来自IEnumerator
{
Console.WriteLine("MOVE");
index++;
if (index == coll.arr.Count)
return false;
else
{
current = coll.arr[index];
return true;
}
}
public void Reset()//这个函数来自IEnumerator
{
Console.WriteLine("RESET");
current = default(T);
index = 0;
}
private MyColl<T> coll;
private int index;
private T current;
}
public class Program
{
public static void Fun()
{
MyColl<int> col = new MyColl<int>(1, 2, 3, 4);
foreach (int i in col)
{
}
}
static void Main()
{
Fun();
Console.WriteLine("-----------------------------------------");
MyColl<int> col=new MyColl<int>(1,2,3,4);
foreach (int i in col)
{
}
Console.WriteLine("-----------------------------------------");
foreach (int i in col)
{
}
}
}
}
[csharp] view plaincopy
</pre><pre name="code" class="csharp">
[csharp] view plaincopy
</pre><pre name="code" class="csharp">
[csharp] view plaincopy
</pre><pre name="code" class="csharp">
如果用yield return的话,就变的简单多了,只要修改一个函数如下;而且NestEnum的类都不需要了
[csharp] view plaincopy
public IEnumerator<T> GetEnumerator()//这个函数来自IEnumerable<T>
{
for (int i = 2; i < arr.Count; i++)
yield return arr[i];
for (int i = 0; i < 2; i++)
yield return arr[i];
}
[csharp] view plaincopy
</pre><p>yield return还有一种不放在类里的用法,很奇怪,注意函数返回值只能是IEnumerable<T>或者是IEnumerable 不能使IEnumerator<T>和IEnumerator,因为需要返回值里面有</p><p>GetEnumerator()方法</p><p><pre name="code" class="csharp"> public class Program
{
static void Main()
{
List<int> intlist = new List<int>();
intlist.Add(1);
intlist.Add(2);
intlist.Add(3);
intlist.Add(4);
foreach(int n in CreateReverseIterator(intlist))
{
Console.WriteLine(n);
}
}
static IEnumerable<T> CreateReverseIterator<T>(IList<T> list)
{
int count = list.Count;
for (int i = count - 1; i >= 0; i--)
{
yield return list[i];
}
}
}