VC语言

本类阅读TOP10

·VC++ 学习笔记(二)
·用Visual C++打造IE浏览器(1)
·每个开发人员现在应该下载的十种必备工具
·教你用VC6做QQ对对碰外挂程序
·Netmsg 局域网聊天程序
·Windows消息大全
·VC++下使用ADO编写数据库程序
·VC++学习笔记(四)
·非法探取密码的原理及其防范
·怎样在VC++中访问、修改注册表

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
在C#中如何支持Foreach

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

    在VB6中,我最喜欢For each语句,并让我的类支持这一特性,自然的我也希望C#中也有此功能,一查资料,马上发现C#支持Forcach,可没介绍任何让自己的类支持Foreach。还是看看MSDN吧。

    MSDN指出必须支持System.Collections.IEnumerable接口,而IEnumerable接口中仅有一个方法:GetEnumerator,他要求返回支持System.Collections.IEnumerator接口的对象引用,一看IEnumerator接口就明白了其工作原理,他有MoveNext、Reset方法和Current属性。

    老实说一开始我真的很不明白MS为什么不是直接支持IEnumerator接口就可以了,为什么要绕个弯呢,后来仔细一想,的确很有道理,叫我说我说不好,大致的意思是在多线程下提供多个状态,不知道对不对,反正我是这样认为的。

    看一下我写的演示代码:

    using System;

 

namespace WindowsApplication3

{

     public class class1

     {

          [STAThread]

          static void Main()

          {

               Demo d=new Demo();

              

               //Foreach演示代码

               foreach(int i in d)

                    Console.WriteLine(i);

          }

     }

 

     /// <summary>支持Foreach的演示类</summary>

     public class Demo:System.Collections.IEnumerable

     {

          //内部要枚举的数据

          int[] intList={1,2,3};

 

          public Demo(){}

 

          //对象支持IEnumerable接口,返回一个支持IEnumerator接口的实例

          public  System.Collections.IEnumerator GetEnumerator()

          {

               DemoForIEnumerator d=new DemoForIEnumerator(this);

               return d;

          }

         

          //索引器,从1开始索引

          public int this[int nIndex]

          {

               get{return intList[nIndex-1];}

          }

 

          //总数

          public int Length

          {

               get{return intList.Length;}

          }

     }

 

     /// <summary>支持IEnumerator接口的类</summary>

     class DemoForIEnumerator:System.Collections.IEnumerator

     {

          Demo mDemo;

          int per =0;

 

          public DemoForIEnumerator(Demo d)

          {mDemo=d;}

 

          //向前移动,无法继续移动时返回false,否则返回true。

          public System.Boolean MoveNext()

          {

               if (per>=mDemo.Length)

                    return false;

               else

               {

                    per++;

                    return true;

               }

          }

 

          //复位

          public void Reset()

          {per=0;}

 

          //当前指针下的值。

          public object Current

          {

               get{return mDemo[per];}

          }

     }

}

 

在这段代码运行时我发现Reset方法根本不会执行,我不知道他什么时候会调用。其实上面的代码如果你不想额外编写一个类,可以在GetEnumerator方法中返回this,然后自己也支持IEnumerator接口就可以了。

 

 




相关文章

相关软件