posts - 77, comments - 54, trackbacks - 0, articles - 0
  IT博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

创建自己的迭代器!!

Posted on 2006-08-03 16:22 东人EP 阅读(223) 评论(0)  编辑 收藏 引用 所属分类: .NET
 1 using  System;
 2 using  System.Collections.Generic;
 3 using  System.Text;
 4
 5 namespace  GenericTest
 6 {
 7      class  Program
 8      {
 9          static   void  Main( string [] args)
10          {
11              索引器
26         }

27     }

28
29
30      // 定义迭代器、索引器类
31      public   class  Persons : System.Collections.IEnumerable
32      {
33          public  Persons( params   string [] Names)
34          {
35             m_Names  =   new   string [Names.Length];
36             Names.CopyTo(m_Names,  0 );
37         }

38
39          public   string [] m_Names;
40          // 索引器
41          public   string   this [ int  index]
42          {
43              get
44              {
45                  return  m_Names[index];
46             }

47              set
48              {
49                 m_Names[index]  =  value;
50             }

51         }

52
53          IEnumerable成员
59     }

60
61      public   class  PersonsEnumerator : System.Collections.IEnumerator
62      {
63          int  index  =   - 1 ;
64         Persons P;
65          public  PersonsEnumerator(Persons P)
66          {
67              this .P  =  P;
68         }

69          IEnumerator成员
97     }

98 }

99
只有注册用户登录后才能发表评论。