日期:2014-05-17  浏览次数:20672 次

请问一个关于IEnumeratalbe的问题!谢谢大家
例如:class person
  {
  public string firstname;
  public string lastname;
  public person(string f,string l)
  {

  this.firstname=f;
  this.lastname=l;
  }
   
  }
  class test
  {
  public static void Main()
  {

  person[] personnew=new person[1]{"john","jack"}

  foreach(person n in personnew)
  console.writeline(n.firstname+n.lastname);

  }



  }
这样就可以实现foreach遍历了,为什么msdn里代码还要实现ienumerable和ienumerator以后进行遍历。

------解决方案--------------------
没有实现此接口你将无法使用foreach。
在foreach中被枚举对象将是只读的,对枚举对象的修改都将导致异常。