C#从foreach语句中枚举元素看数组详解

2019-12-30 17:29:24王振洲

C#的foreach语句不会解析为IL代码中的foreach语句,C#编译器会把foreach语句转换为IEnumerable接口的方法和属性,foreach语句使用IEnumerator接口的方法和属性,迭代数组中的所有元素,为此,IEnumerator定义了Current属性,来返回光标所在的元素,该接口的MoveNext()方法移动到数组的下一个元素上,如果有这个元素该方法就返回true否则返回false,这个接口的泛型版本IEnumerator派生自接口IDisposable,因此定义了Dispose()方法来清理枚举器占用的资源,使用foreach语句会解析为下面的代码段


 IEnumerator enumerator = persons.GetEnumerator();
   while (enumerator.MoveNext())
   {
    var person = enumerator.Current;
    Console.WriteLine(person);
   }

为了方便的创建枚举器,C#添加了yield语句,yield return 语句返回集合的一个元素,并移动到下一个元素,yield break 可停止迭代。使用迭代块,编译器会生成一个yield类型,其中包含一个状态机,如下代码段所示。yield 类型实现IEnumerator和IDisposable接口的属性和方法。在下面的例子中,可以把yield类型看作内部类Enumerator.外部类的GetEnumerator()方法实例化并返回一个新的yield类型。在yield类型中,变量state定义了迭代的当前位置,每次调用MoveNext()时,当前位置都会改变,MoveNext()封装了迭代代码,并设置了current变量的值,从而使Current属性根据位置返回一个对象。


 static void Main(string[] args)
  {
   var helloCollection = new HelloCollection();
   foreach (string s in helloCollection)
   {
    Console.WriteLine(s);
   }
  }

  public class HelloCollection
  {
   public IEnumerator<string> GetEnumerator()
   {
    yield return "Hello";
    yield return "World";
   }
  }
  public class HelloCollectionOther
  {
   public IEnumerator GetEnumertor()
   {
    return new Enumerator(0);
   }
   public class Enumerator : IEnumerator<string>, IEnumerator, IDisposable
   {
    private int state;
    private string current;
    public Enumerator(int state)
    {
     this.state = state;
    }

    public string Current => throw new NotImplementedException();

    object IEnumerator.Current
    {
     get { return current; }
    }

    public void Dispose()
    {
     throw new NotImplementedException();
    }

    public bool MoveNext()
    {
     switch (state)
     {
      case 0:current = "hello";
       state = 1;
       return true;
      case 1:current = "world";
       state = 2;
       return true;
      case 2:
       break;
     }
     return false;
    }

    public void Reset()
    {
     throw new NotImplementedException();
    }
   }
  }

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对ASPKU的支持。