c#开发园地

c#开发园地

管理

在C#中,凡是实现了IEnumerator接口的数据类型都可以用foreach语句进行迭代访问,可是,对于自定义类型如何实现这个接口以支持foreach的迭代呢?要实现这个功能,先来看看IEnumerableIEnumerator接口的定义:

public interface IEnumerable
{
      
//IEnumerable只有一个方法,返回可循环访问集合的枚举数。
      IEnumerator GetEnumerator()  ;
}

 

public interface IEnumerator
{  
      
// 方法
      //移到集合的下一个元素。如果成功则返回为 true;如果超过集合结尾,则返回false
      bool MoveNext();
     // 将集合设置为初始位置,该位置位于集合中第一个元素之前
      
void Reset();

      
// 属性:获取集合中的当前元素
      object Current { get; }
}

如果我们的自定义数据类型派生于这两个接口,并实现接口中的方法,即可用foreach进行迭代。

下面是一个实现的简单例子:

using System;
using System.Collections ;
using System.Collections.Generic;
using System.Text;

namespace IEnumberableTest
{
    
class Program
    {
        
static void Main(string[] args)
        {
            myEnum enum1 
= new myEnum(20);

            
foreach ( point p in enum1)
            {
                Console.WriteLine(
"("+p.x .ToString ()+","+p.y.ToString ()+","+p.z.ToString ()+")");
            }

            Console.Read();
        }
    }

    
//一个结构体,用于类myEnum
    struct point
    {
        
public int x;
        
public int y;
        
public int z;
    }
    //我们的一个派生于IEnumerable和IEnumerator接口的自定义类
    
class myEnum :IEnumerable,IEnumerator 
    {
        
//定义索引
        private int index;

        
//定义一个point结构的数组
        private point[] points;

        
//类的构造函数,用于初始化point结构数组
        public myEnum(int numofpoint)
        {
            
this.index = -1;
            points 
= new point[numofpoint];

            
for (int i = 0; i < points.Length ; i++)
            {
                points[i].x 
= i;
                points[i].y 
= i*i;
                points[i].z 
= i*i*i;
            }
        }

        
//实现IEnumerable接口的GetEnumerator方法,返回一个IEnumerator,这里返回我们的自定义类,因为要对这个类的对象进行迭代
        public IEnumerator GetEnumerator()
        {
            
return (IEnumerator)this;        这里太让我激动了,原来就是返回this这么简单,因为this本身已经是一个ienumerator了, 不过唯一的问题在于当重复迭代这个对象时,index整型字段还原成-1的问题,所以要在movenext()方法return false之前还原index的值。                                                                        
        }

        
//实现IEnumerator接口的Reset方法,将集合索引置于第一个元素之前
        public void Reset()
        {
            index 
= -1;
        }

        
//实现IEnumerator接口的Current属性,返回一个自定义的point结构,即point数组的第index元素
        public object Current
        {
            
get
            {
                
return points[index];
            }
        }

        
//实现IEnumerator接口的MoveNext方法,用于向前访问集合元素,如果超出集合范围,返回false
        public bool MoveNext()
        {
            index
++;
            
if(index >= points.Length)
               {index=-1; return false;}         再返回false之前还原index的值(-1)
            else 
                return
true;
        }

    }
}

编译这个例子,可以看到运行结果。

posted on 2008-02-06 22:19  chl  阅读(3297)  评论(0)    收藏  举报