C#IEnumerable和IEnumerator的学习心得
2011-02-24 23:35
246 查看
下面是一个例子:
/// <summary>
/// 定义人的类型
/// </summary>
public class Person
{
public string firstName;
public string lastName;
public Person(string first, string last)
{
this.firstName = first;
this.lastName = last;
}
}
/// <summary>
/// 实现枚举器
/// </summary>
public class MyEnum<T> : IEnumerator
{
public T[] _array;
int _position = -1;
public MyEnum(T[] _array)
{
this._array = _array;
}
public object Current
{
get
{
try
{
return _array[_position];
}
catch (IndexOutOfRangeException)
{
throw new IndexOutOfRangeException();
}
}
}
public bool MoveNext()
{
_position++;
return (_position < _array.Length);
}
public void Reset()
{
_position = -1;
}
}
/// <summary>
/// 实现一个可枚举的类
/// </summary>
public class MyList<T> : IEnumerable
{
public T[] _array;
public MyList(T[] _array)
{
this._array = _array;
}
//取得枚举器
public IEnumerator GetEnumerator()
{
return new MyEnum<T>(this._array);
}
}
class TestMain
{
public static void Main()
{
Person[] personArray = {
new Person("John", "Smith"),
new Person("Jim", "Johnson"),
new Person("Sue", "Rabon"),
};
MyList<Person> list = new MyList<Person>(personArray);
foreach (Person p in list)
Console.WriteLine(p.firstName + " " + p.lastName);
Console.ReadKey();
}
}
结论:如果将foreach语句用于集合,就需要IEnumerable接口的GetEnumerator()方法放回一个IEnumerator枚举。
也可以使用yield语句可以快速的实现一个枚举IEnumerator,例子如下:
/// <summary>
/// 定义人的类型
/// </summary>
public class Person
{
public string firstName;
public string lastName;
public Person(string first, string last)
{
this.firstName = first;
this.lastName = last;
}
}
/// <summary>
/// 实现一个可枚举的类
/// </summary>
public class MyList<T> : IEnumerable
{
public T[] _array;
public MyList(T[] _array)
{
this._array = _array;
}
//取得枚举器
public IEnumerator GetEnumerator()
{
foreach (T t in _array)
{
yield return t;
}
}
}
class TestMain
{
public static void Main()
{
Person[] personArray = {
new Person("John", "Smith"),
new Person("Jim", "Johnson"),
new Person("Sue", "Rabon"),
};
MyList<Person> list = new MyList<Person>(personArray);
foreach (Person p in list)
Console.WriteLine(p.firstName + " " + p.lastName);
Console.ReadKey();
}
}
这个更简单
/// <summary>
/// 定义人的类型
/// </summary>
public class Person
{
public string firstName;
public string lastName;
public Person(string first, string last)
{
this.firstName = first;
this.lastName = last;
}
}
/// <summary>
/// 实现枚举器
/// </summary>
public class MyEnum<T> : IEnumerator
{
public T[] _array;
int _position = -1;
public MyEnum(T[] _array)
{
this._array = _array;
}
public object Current
{
get
{
try
{
return _array[_position];
}
catch (IndexOutOfRangeException)
{
throw new IndexOutOfRangeException();
}
}
}
public bool MoveNext()
{
_position++;
return (_position < _array.Length);
}
public void Reset()
{
_position = -1;
}
}
/// <summary>
/// 实现一个可枚举的类
/// </summary>
public class MyList<T> : IEnumerable
{
public T[] _array;
public MyList(T[] _array)
{
this._array = _array;
}
//取得枚举器
public IEnumerator GetEnumerator()
{
return new MyEnum<T>(this._array);
}
}
class TestMain
{
public static void Main()
{
Person[] personArray = {
new Person("John", "Smith"),
new Person("Jim", "Johnson"),
new Person("Sue", "Rabon"),
};
MyList<Person> list = new MyList<Person>(personArray);
foreach (Person p in list)
Console.WriteLine(p.firstName + " " + p.lastName);
Console.ReadKey();
}
}
结论:如果将foreach语句用于集合,就需要IEnumerable接口的GetEnumerator()方法放回一个IEnumerator枚举。
也可以使用yield语句可以快速的实现一个枚举IEnumerator,例子如下:
/// <summary>
/// 定义人的类型
/// </summary>
public class Person
{
public string firstName;
public string lastName;
public Person(string first, string last)
{
this.firstName = first;
this.lastName = last;
}
}
/// <summary>
/// 实现一个可枚举的类
/// </summary>
public class MyList<T> : IEnumerable
{
public T[] _array;
public MyList(T[] _array)
{
this._array = _array;
}
//取得枚举器
public IEnumerator GetEnumerator()
{
foreach (T t in _array)
{
yield return t;
}
}
}
class TestMain
{
public static void Main()
{
Person[] personArray = {
new Person("John", "Smith"),
new Person("Jim", "Johnson"),
new Person("Sue", "Rabon"),
};
MyList<Person> list = new MyList<Person>(personArray);
foreach (Person p in list)
Console.WriteLine(p.firstName + " " + p.lastName);
Console.ReadKey();
}
}
这个更简单
相关文章推荐
- [7]深度学习和Keras---- 快速入门心得
- private(学习心得)
- WCF学习心得------(七)消息协定
- 学习心得之 Android五子棋大战
- C++学习心得
- 设计模式之装饰模式,学习心得。如有错误,请多指教,谢谢。
- php学习心得1
- java反射机制的学习(综合各位前辈的心得)
- iOS:学习runtime的理解和心得
- Angular学习心得之directive——require选项的细节
- C/C++ 多线程的学习心得总结
- 学习心得之keil C51
- 学习Android一点心得
- android Gallery的学习心得
- JavaScript学习心得之概述
- 学习JAVA框架的心得体会
- 【转】关于diplay学习心得
- FieldInfo学习心得
- Win32调试API学习心得(一)
- 学习Oracle认证的一些心得技巧