C# 枚举器 手动实现枚举器 可枚举集合 枚举器操作 迭代器

在C#语言中提供 foreach 查询操作,foreach 大大的简化了要编写的代码,但是foreach 只能用来遍历一个可枚举的集合(enumable),可枚举的集合就是实现了System.Collections.IEnumerable接口的一个集合。

 

但是foreach 返回的是可枚举对象的一个只读集合,不可以通过foreach 来修改可枚举对象。

 

简单的一个例子:

[html]  view plain copy
  1. int[] number_array = new int[] { 1,2,3,4,5,6};  
  2.   
  3. foreach (int k in number_array)  
  4. {  
  5.     Console.WriteLine(k);  
  6. }  


int 类型,继承了IEnumerable<T> 接口,所有可以用foreach 来遍历。如果我们自己写的一个类,没有实现IEnumerable<T> 接口,用foreach 就不能进行迭代了,编译器会报错。

 

例如:

自己写的一个类:

[html]  view plain copy
  1. class Student: IComparable<Student>  
  2.    {  
  3.        public Student()  
  4.        {  
  5.            // nothing  
  6.        }  
  7.   
  8.        private int age;  
  9.   
  10.        public int Age  
  11.        {  
  12.            get { return this.age;}  
  13.            set { this.age = value; }  
  14.        }  
  15.   
  16.        public int CompareTo(Student other)  
  17.        {  
  18.            //throw new NotImplementedException();  
  19.            if (this.age == other.age)  
  20.            {  
  21.                return 0;  
  22.            }  
  23.            else if (this.age < other.age)  
  24.            {  
  25.                return -1;  
  26.            }  
  27.            else  
  28.            {  
  29.                return 1;  
  30.            }  
  31.        }  
  32.    }  


 

这个类实现了 IComparable<T> 接口,但是没有实现 IEnumerable<T> 接口,不能用foreach 遍历。

下面的代码对这个没有实现 IEnumerable<T> 接口的类进行foreach 遍历结果发生编译错误。

 

[html]  view plain copy
  1. //Student[] student=new Student[5];  
  2. //student.test();  
  3. //foreach (Student t in student)  
  4. //{  
  5.                  
  6. //}  


下面我们将自己手动的给上面那个简单的类实现枚举器,通过foreach来遍历这个类。

 

在 IEnumerable接口中,包括一个GetEnumerator的方法:

[html]  view plain copy
  1. IEnumerator GetEnumertor();   


 

这个方法返回一个枚举器对象。

IEnumerator 接口中规定了以下方法和属性:

[html]  view plain copy
  1. object Current {get;}  
  2. bool MoveNext();  
  3. void Reset();  


最初枚举器的指针指向枚举集合的第一个元素的前面,在获取 Current之前先要调用MoveNext() 使指针指向下一个元素。

 

以上所说的接口, 都有他们对应的泛型接口,在接下来的实现中,都会使用泛型接口。

当我们给要枚举的类实现了IEnumerable<T>接口后,foreach语句会自动的让生成一个枚举器,并通过枚举器中的Current, MoveNext等获取下一个数据,这一切过程对于foreach的使用者都是透明的。

 

下面,先给出本实例的所有代码,然后进行分析讲解。

所有原码如下:

[html]  view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace EnumerableTest  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             int[] number_array = new int[] { 1,2,3,4,5,6};  
  13.   
  14.             foreach (int k in number_array)  
  15.             {  
  16.                 Console.WriteLine(k);  
  17.             }  
  18.   
  19.             //Student[] student=new Student[5];  
  20.             student.test();  
  21.             //foreach (Student t in student)  
  22.             //{  
  23.                   
  24.             //}  
  25.   
  26.             Student[] student = new Student[5];  
  27.             student[0] = new Student();  
  28.             student[0].Age = 12;  
  29.             student[1] = new Student();  
  30.             student[1].Age = 13;  
  31.             student[2] = new Student();  
  32.             student[2].Age = 74;  
  33.             student[3] = new Student();  
  34.             student[3].Age = 34;  
  35.             student[4] = new Student();  
  36.             student[4].Age = 32;  
  37.   
  38.             Console.WriteLine("/");  
  39.   
  40.             Sector<Student> sector = new Sector<Student>(student);  
  41.             foreach (Student t in sector)  
  42.             {  
  43.                 Console.WriteLine(t.Age);  
  44.             }  
  45.         }  
  46.     }  
  47.   
  48.     class Sector<T> : IEnumerable<T>  
  49.         where T : IComparable<T>  
  50.     {  
  51.         T[] student;  
  52.   
  53.         public Sector(T[] t)  
  54.         {  
  55.             student = t;  
  56.         }  
  57.   
  58.         public IEnumerator<T> GetEnumerator()  
  59.         {  
  60.             return new SectorEnumator<T>(student);  
  61.             //throw new NotImplementedException();  
  62.         }  
  63.   
  64.         System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()  
  65.         {  
  66.             //throw new NotImplementedException();  
  67.             return null;  
  68.         }  
  69.     }  
  70.   
  71.     class SectorEnumator<T> : IEnumerator<T>  
  72.         where T : IComparable<T>  
  73.     {  
  74.         private T[] student;  
  75.         private Queue<T> MyQueue;  
  76.         private T currentItem;  
  77.   
  78.         public SectorEnumator(T[] s)  
  79.         {  
  80.             this.student = s;  
  81.         }  
  82.   
  83.         public T Current  
  84.         {  
  85.             get { return this.currentItem; }  
  86.         }  
  87.   
  88.         public void Dispose()  
  89.         {  
  90.             // simply do nothing  
  91.             //throw new NotImplementedException();  
  92.         }  
  93.   
  94.         object System.Collections.IEnumerator.Current  
  95.         {  
  96.             get { throw new NotImplementedException(); }  
  97.         }  
  98.   
  99.         public bool MoveNext()  
  100.         {  
  101.             if (this.MyQueue == null)  
  102.             {  
  103.                 MyQueue = new Queue<T>();  
  104.                 populate(student);  
  105.             }  
  106.             if (this.MyQueue.Count > 0)  
  107.             {  
  108.                 this.currentItem = MyQueue.Dequeue();  
  109.                 return true;  
  110.             }  
  111.             return false;  
  112.             //throw new NotImplementedException();  
  113.         }  
  114.   
  115.         public void Reset()  
  116.         {  
  117.             //throw new NotImplementedException();  
  118.             MyQueue.Clear();  
  119.             populate(student);  
  120.         }  
  121.   
  122.         private void populate(T[] s)  
  123.         {  
  124.             int num = s.Length;  
  125.             for (int i = 0; i < num; i++)  
  126.             {  
  127.                 MyQueue.Enqueue(s[i]);  
  128.             }  
  129.         }  
  130.     }  
  131.   
  132.     class Student: IComparable<Student>  
  133.     {  
  134.         public Student()  
  135.         {  
  136.             // nothing  
  137.         }  
  138.   
  139.         private int age;  
  140.   
  141.         public int Age  
  142.         {  
  143.             get { return this.age;}  
  144.             set { this.age = value; }  
  145.         }  
  146.   
  147.         public int CompareTo(Student other)  
  148.         {  
  149.             //throw new NotImplementedException();  
  150.             if (this.age == other.age)  
  151.             {  
  152.                 return 0;  
  153.             }  
  154.             else if (this.age < other.age)  
  155.             {  
  156.                 return -1;  
  157.             }  
  158.             else  
  159.             {  
  160.                 return 1;  
  161.             }  
  162.         }  
  163.     }  
  164. }  


首先,我们定义了一个Student 类,并实现了IComparable<T> 接口,我们程序的目的就是在使用Student类的时候,可以通过foreach 来遍历Student数组中的每个成员,但是默认情况下Student类并没有实现IEnumberable<T>接口,所有在直接用foreach 遍历的时候会编译报错。

 

为了遍历Student类,我们手动实现了一个枚举器。

[html]  view plain copy
  1. class SectorEnumator<T> : IEnumerator<T>  
  2.         where T : IComparable<T>  
  3.     {  
  4.         private T[] student;  
  5.         private Queue<T> MyQueue;  
  6.         private T currentItem;  
  7.   
  8.         public SectorEnumator(T[] s)  
  9.         {  
  10.             this.student = s;  
  11.         }  
  12.   
  13.         public T Current  
  14.         {  
  15.             get { return this.currentItem; }  
  16.         }  
  17.   
  18.         public void Dispose()  
  19.         {  
  20.             // simply do nothing  
  21.             //throw new NotImplementedException();  
  22.         }  
  23.   
  24.         object System.Collections.IEnumerator.Current  
  25.         {  
  26.             get { throw new NotImplementedException(); }  
  27.         }  
  28.   
  29.         public bool MoveNext()  
  30.         {  
  31.             if (this.MyQueue == null)  
  32.             {  
  33.                 MyQueue = new Queue<T>();  
  34.                 populate(student);  
  35.             }  
  36.             if (this.MyQueue.Count > 0)  
  37.             {  
  38.                 this.currentItem = MyQueue.Dequeue();  
  39.                 return true;  
  40.             }  
  41.             return false;  
  42.             //throw new NotImplementedException();  
  43.         }  
  44.   
  45.         public void Reset()  
  46.         {  
  47.             //throw new NotImplementedException();  
  48.             MyQueue.Clear();  
  49.             populate(student);  
  50.         }  
  51.   
  52.         private void populate(T[] s)  
  53.         {  
  54.             int num = s.Length;  
  55.             for (int i = 0; i < num; i++)  
  56.             {  
  57.                 MyQueue.Enqueue(s[i]);  
  58.             }  
  59.         }  
  60.     }  


在MOveNext() 中,如果这个枚举器的MyQueue为null,则new 一个Queue, 用MyQueue存放给枚举器传进来的对象数组。每一次MoveNext() 都会造成一个对象出队。

 

然后给一个类实现IEnumerable<T> 接口,通过GetEnumerator() 方法获取枚举器:

 

[html]  view plain copy
  1. class Sector<T> : IEnumerable<T>  
  2.         where T : IComparable<T>  
  3.     {  
  4.         T[] student;  
  5.   
  6.         public Sector(T[] t)  
  7.         {  
  8.             student = t;  
  9.         }  
  10.   
  11.         public IEnumerator<T> GetEnumerator()  
  12.         {  
  13.             return new SectorEnumator<T>(student);  
  14.             //throw new NotImplementedException();  
  15.         }  
  16.   
  17.         System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()  
  18.         {  
  19.             //throw new NotImplementedException();  
  20.             return null;  
  21.         }  
  22.     }  


这样,在我们以后创建一个Sector 对象以后,就可以直接用foreach 对Sector对象中的Student 对象进行枚举:

[html]  view plain copy
  1. Sector<Student> sector = new Sector<Student>(student);  
  2.            foreach (Student t in sector)  
  3.            {  
  4.                Console.WriteLine(t.Age);  
  5.            }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值