帝国CMS模板大全
www.admin99.cn
www.92cms.cn 帝国CMS模板下载站!,情怀,养站,二次开发!源码需求比较大的一站式会员下载,价更省!!!

C# IEnumerable和IEnumerator接口浅析

温故而知新,可以为师矣,有空经常复习一下基础知识是有必要的,并且能加深理解和记忆。

foreach常用于循环访问集合,对实现ienumerable的接口的容器进行遍历,ienumerable和ienumerator接口我有时候也有点迷糊,按官方的解释,ienumerable是枚举器接口,ienumerator是迭代器接口,从字面意思来看相差不大,逐一分析一下。

ienumerable接口

?

public interface ienumerable

{

  ienumerator getenumerator();

}

继承ienumerable接口的类需实现暴露出来的getenumerator()方法,并返回一个ienumerator接口对象,看来真正做事的是ienumerator,f12看一下ienumerator又有什么鬼东西。

ienumerator接口

?

public interface ienumerator

{

  object current { get ; }

  bool movenext();

  void reset();

}

ienumerator接口有三个东东,一个属性current,返回当前集合中的元素,方法movenext()移动到下一个,遍历不都是向后遍历的嘛,reset(),字面意思重置,这个容易理解。做个假设: 既然ienumerable接口返回是ienumerator接口迭代器来实现的,那么仅继承ienumerator迭代器接口能不能实现一个自定义容器?

定义一个 phone类

?

public class phone

{

  public string name;

  public phone( string name)

  {

   this .name = name;

  }

}

定义一个名为 myenumerator 迭代器,并现实它接口ienumerator

?

public class myenumerator : ienumerator

{

  phone[] p;

  int idx = -1;

  public myenumerator(phone[] t)

  {

   p = t;

  }

  public object current

  {

   get

   {

   if (idx == -1)

    return new indexoutofrangeexception();

   return p[idx];

   }

  }

  public bool movenext()

  {

   idx++;

   return p.length > idx;

  }

  public void reset()

  {

   idx = -1;

  }

}

?

class program

  {

  static void main( string [] args)

  {

        show( "———–ienumerator————" );

   phone[] phones = new phone[] { new phone( "iphone 7s" ), new phone( "iphone 6s" ), new phone( "iphone 5s" ) };

   myenumerator enumerator = new myenumerator(phones);

   while (enumerator.movenext())

   {

   phone p = enumerator.current as phone;

   show(p.name);

   }

   console.readkey();

  }

  static void show( string i)

  {

   console.writeline(i);

  }

  }

结果显示:

果然不出所料,真正做事情的是ienumerator接口,即可循环访问自定义的一个容器,不过,初衷是想用foreach来做循环访问、遍历的。那好,那就只能显示ienumerable接口来做。稍稍改造一下phone类:

?

public class phone : ienumerable

  {

  public string name ;

  public phone( string name)

  {

   this .name = name;

  }

  phone[] p;

  public phone(phone[] t)

  {

   p = t;

  }

  public ienumerator getenumerator()

  {

   return new myenumerator(p);

  }

  }

?

static void main( string [] args)

  {

   show( "———–ienumerator————" );

   phone[] phones = new phone[] { new phone( "iphone 7s" ), new phone( "iphone 6s" ), new phone( "iphone 5s" ) };

   myenumerator enumerator = new myenumerator(phones);

   while (enumerator.movenext())

   {

   phone p = enumerator.current as phone;

   show(p.name);

   }

   show( "———–ienumerable————" );

   phone phonelist = new phone(phones);

   foreach (phone p in phonelist)

   {

   show(p.name);

   }

   console.readkey();

  }

结果显示:

大功告成,再扩展成通用的容器 phonepackage ,继承泛型ienumerable<t>接口即可。

?

public class phonepackage<t> : ienumerable<t>

  {

  private list<t> datalist = null ;

  public void add(t t)

  {

   if (datalist == null )

   datalist = new list<t>();

   datalist.add(t);

  }

  public ienumerator<t> getenumerator()

  {

   foreach (t t in datalist)

   {

   yield return t;

   }

  }

  ienumerator ienumerable.getenumerator()

  {

   foreach (t t in datalist)

   {

   yield return t;

   }

  }

  }

?

static void main( string [] args)

  {

   show( "———–ienumerator————" );

   phone[] phones = new phone[] { new phone( "iphone 7s" ), new phone( "iphone 6s" ), new phone( "iphone 5s" ) };

   myenumerator enumerator = new myenumerator(phones);

   while (enumerator.movenext())

   {

   phone p = enumerator.current as phone;

   show(p.name);

   }

   show( "———–ienumerable————" );

   phone phonelist = new phone(phones);

   foreach (phone p in phonelist)

   {

   show(p.name);

   }

   show( "———–ienumerable<t>————" );

   phonepackage<phone> phonepackage = new phonepackage<phone>();

   phonepackage.add( new phone( "iphone 7s" ));

   phonepackage.add( new phone( "iphone 6s" ));

   phonepackage.add( new phone( "iphone 5s" ));

   foreach (phone p in phonepackage)

   {

   show(p.name);

   }

   console.readkey();

  }

  static void show( string i)

  {

   console.writeline(i);

  }

结果显示:

ienumerator迭代器接口挺啰嗦的,yield是简化了遍历的语法糖而已。

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持服务器之家!

原文链接:http://www.cnblogs.com/EminemJK/p/6428282.html

dy(“nrwz”);

赞(0)
版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
文章名称:《C# IEnumerable和IEnumerator接口浅析》
文章链接:https://www.admin99.cn/7795
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。
QQ站长交流群:953950264

登录

找回密码

注册