|
|
@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
|
|
using System.Collections;
|
|
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace LinqStudy.IEnumerableDemo
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// 普通学生类
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
public class Student
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
|
|
|
public int Age { get; set; }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// 实现可枚举接口
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
public class Students : IEnumerable
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
private Student[] arrStudent;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Students(Student[] collection)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
arrStudent = collection;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Student this[int index]
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
get { return arrStudent[index]; }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public int Count
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
get { return arrStudent.Length; }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// 实现接口
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
|
|
IEnumerator IEnumerable.GetEnumerator()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return new StudentIterator(this);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// 实现枚举器接口
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
public class StudentIterator : IEnumerator
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
private Students arrStudents;
|
|
|
|
|
|
|
|
private int idx;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
internal StudentIterator(Students collection)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
this.arrStudents = collection;
|
|
|
|
|
|
|
|
idx = -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
object IEnumerator.Current
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
get { return arrStudents[idx]; }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public bool MoveNext()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
idx++;
|
|
|
|
|
|
|
|
return idx < arrStudents.Count;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Reset()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
idx = -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose() { }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//使用
|
|
|
|
|
|
|
|
public class UseDemo
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
public void Test()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Students arrStudent = new Students
|
|
|
|
|
|
|
|
(
|
|
|
|
|
|
|
|
new Student[]
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
new Student() { Name = "张三", Age = 23 },
|
|
|
|
|
|
|
|
new Student() { Name = "李四", Age = 24 },
|
|
|
|
|
|
|
|
new Student() { Name = "王五", Age = 45 },
|
|
|
|
|
|
|
|
new Student() { Name = "DDD", Age = 21 },
|
|
|
|
|
|
|
|
new Student() { Name = "BBB", Age = 14 },
|
|
|
|
|
|
|
|
new Student() { Name = "CCC", Age = 3 },
|
|
|
|
|
|
|
|
new Student() { Name = "AAA", Age = 22 },
|
|
|
|
|
|
|
|
new Student() { Name = "BBB", Age = 55 },
|
|
|
|
|
|
|
|
new Student() { Name = "CCC", Age = 43 },
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach (Student obj in arrStudent)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Console.WriteLine("姓名:" + obj.Name + ",年龄:" + obj.Age);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|