IEnumerable 和 IEnumerator 接口

实现IEnumerable接口的类,可以支持foreach循环遍历对象的集合元素

IEnumerable:
IEnumerator GetEnumerator()返回可循环访问集合的枚举数。

IEnumerator:
object Current获取集合中的当前元素。
bool MoveNext()将枚举数推进到集合的下一个元素。
如果枚举数成功地推进到下一个元素,则为 true;如果枚举数越过集合的结尾,则为 false
void Reset()将枚举数设置为其初始位置,该位置位于集合中第一个元素之前。


sample:
None.gif using  System;
None.gif
using  System.Collections;
None.gif
None.gif
namespace  ConsoleApplication1
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Summary description for Class1.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    class Class1
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// The main entry point for the application.
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [STAThread]
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            CStringEnum se 
= new CStringEnum();
InBlock.gif
InBlock.gif            
foreach (string s in se)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(s);
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            Console.Read() ;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
class CStringEnum : IEnumerable, IEnumerator
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
string[] items = new string[16];
InBlock.gif        
int index = -1;
InBlock.gif
InBlock.gif        
public CStringEnum()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
for(int i=0; i<items.Length; ++i)
InBlock.gif                items[i] 
= "s" + i.ToString();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
IEnumerable Members#region IEnumerable Members
InBlock.gif
InBlock.gif        
public IEnumerator GetEnumerator()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// TODO:  Add StringCollection.GetEnumerator implementation
InBlock.gif
            return (IEnumerator)this;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
IEnumerator Members#region IEnumerator Members
InBlock.gif
InBlock.gif        
public void Reset()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// TODO:  Add CStringEnum.Reset implementation
InBlock.gif
            index = -1;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public object Current
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
// TODO:  Add CStringEnum.Current getter implementation
InBlock.gif
                return items[index];
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public bool MoveNext()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// TODO:  Add CStringEnum.MoveNext implementation
InBlock.gif
            index++;
InBlock.gif            
return index >= items.Length ? false : true;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/netflu/archive/2006/07/31/463974.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值