C#中foreach实现原理详解

 更新时间:2017年09月27日 09:22:09   作者:阿珑  
这篇文章主要为大家详细介绍了C#中foreach实现原理,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文主要记录我在学习C#中foreach遍历原理的心得体会。

对集合中的要素进行遍历是所有编码中经常涉及到的操作,因此大部分编程语言都把此过程写进了语法中,比如C#中的foreach。经常会看到下面的遍历代码:

var lstStr = new List<string> { "a", "b" };
   foreach (var str in lstStr)
      {
        Console.WriteLine(str);
      }

实际此代码的执行过程:

var lstStr = new List<string> {"a", "b"};
   IEnumerator<string> enumeratorLst = lstStr.GetEnumerator();
   while (enumeratorLst.MoveNext())
      {
        Console.WriteLine(enumeratorLst.Current);
      }

会发现有GetEnumerator()方法和IEnumerator<string>类型,这就涉及到可枚举类型和枚举器的概念。

为了方便理解,以下为非泛型示例:

// 摘要:
//   公开枚举器,该枚举器支持在非泛型集合上进行简单迭代。
  public interface IEnumerable
  {
    // 摘要:
    //   返回一个循环访问集合的枚举器。
    //
    // 返回结果:
    //   可用于循环访问集合的 System.Collections.IEnumerator 对象。
    IEnumerator GetEnumerator();
  }

实现了此接口的类称为可枚举类型,是可以用foreach进行遍历的标志。

方法GetEnumerator()的返回值是枚举器,可以理解为游标。

// 摘要:
//   支持对非泛型集合的简单迭代。
  public interface IEnumerator
  {
    // 摘要:
    //   获取集合中的当前元素。
    //
    // 返回结果:
    //   集合中的当前元素。
    //
    // 异常:
    //  System.InvalidOperationException:
    //   枚举数定位在该集合的第一个元素之前或最后一个元素之后。
    object Current { get; }

    // 摘要:
    //   将枚举数推进到集合的下一个元素。
    //
    // 返回结果:
    //   如果枚举数成功地推进到下一个元素,则为 true;如果枚举数越过集合的结尾,则为 false。
    //
    // 异常:
    //  System.InvalidOperationException:
    //   在创建了枚举数后集合被修改了。
    bool MoveNext();
    //
    // 摘要:
    //   将枚举数设置为其初始位置,该位置位于集合中第一个元素之前。
    //
    // 异常:
    //  System.InvalidOperationException:
    //   在创建了枚举数后集合被修改了。
    void Reset();
  }

以下是自定义一个迭代器的示例(https://msdn.microsoft.com/en-us/library/system.collections.ienumerator.aspx):

using System;
using System.Collections;

// Simple business object.
public class Person
{
  public Person(string fName, string lName)
  {
    this.firstName = fName;
    this.lastName = lName;
  }

  public string firstName;
  public string lastName;
}

// Collection of Person objects. This class
// implements IEnumerable so that it can be used
// with ForEach syntax.
public class People : IEnumerable
{
  private Person[] _people;
  public People(Person[] pArray)
  {
    _people = new Person[pArray.Length];

    for (int i = 0; i < pArray.Length; i++)
    {
      _people[i] = pArray[i];
    }
  }

// Implementation for the GetEnumerator method.
  IEnumerator IEnumerable.GetEnumerator()
  {
    return (IEnumerator) GetEnumerator();
  }

  public PeopleEnum GetEnumerator()
  {
    return new PeopleEnum(_people);
  }
}

// When you implement IEnumerable, you must also implement IEnumerator.
public class PeopleEnum : IEnumerator
{
  public Person[] _people;

  // Enumerators are positioned before the first element
  // until the first MoveNext() call.
  int position = -1;

  public PeopleEnum(Person[] list)
  {
    _people = list;
  }

  public bool MoveNext()
  {
    position++;
    return (position < _people.Length);
  }

  public void Reset()
  {
    position = -1;
  }

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

  public Person Current
  {
    get
    {
      try
      {
        return _people[position];
      }
      catch (IndexOutOfRangeException)
      {
        throw new InvalidOperationException();
      }
    }
  }
}

class App
{
  static void Main()
  {
    Person[] peopleArray = new Person[3]
    {
      new Person("John", "Smith"),
      new Person("Jim", "Johnson"),
      new Person("Sue", "Rabon"),
    };

    People peopleList = new People(peopleArray);
    foreach (Person p in peopleList)
      Console.WriteLine(p.firstName + " " + p.lastName);

  }
}

/* This code produces output similar to the following:
 *
 * John Smith
 * Jim Johnson
 * Sue Rabon
 *
 */

在有了yield这个关键字以后,我们可以通过这样的方式来创建枚举器:

using System;
using System.Collections;

// Simple business object.
public class Person
{
  public Person(string fName, string lName)
  {
    this.firstName = fName;
    this.lastName = lName;
  }

  public string firstName;
  public string lastName;
}

// Collection of Person objects. This class
// implements IEnumerable so that it can be used
// with ForEach syntax.
public class People : IEnumerable
{
  private Person[] _people;

  public People(Person[] pArray)
  {
    _people = new Person[pArray.Length];

    for (int i = 0; i < pArray.Length; i++)
    {
      _people[i] = pArray[i];
    }
  }

  // Implementation for the GetEnumerator method.
  IEnumerator IEnumerable.GetEnumerator()
  {
    for (int i = 0; i < _people.Length; i++)
    {
      yield return _people[i];
    }
  }

}


class App
{
  static void Main()
  {
    Person[] peopleArray = new Person[3]
    {
      new Person("John", "Smith"),
      new Person("Jim", "Johnson"),
      new Person("Sue", "Rabon"),
    };

    People peopleList = new People(peopleArray);
    foreach (Person p in peopleList)
      Console.WriteLine(p.firstName + " " + p.lastName);
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Unity TextMeshPro实现富文本超链接默认字体追加字体

    Unity TextMeshPro实现富文本超链接默认字体追加字体

    这篇文章主要为大家介绍了Unity TextMeshPro实现富文本超链接默认字体追加字体示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-01-01
  • C#简单发送email的方法

    C#简单发送email的方法

    这篇文章主要介绍了C#简单发送email的方法,涉及C#发送Email的相关技巧,非常具有实用价值,需要的朋友可以参考下
    2015-04-04
  • Unity登录注册时限制发送验证码次数功能的解决方法

    Unity登录注册时限制发送验证码次数功能的解决方法

    这篇文章主要为大家详细介绍了Unity登录注册时限制发送验证码次数功能的解决方案,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-01-01
  • 带你一文了解C#中的LINQ

    带你一文了解C#中的LINQ

    c#提供的ling查询极大的遍历了集合的查询过程,且使用简单方便,非常的有用,下面这篇文章主要给大家介绍了关于C#中LINQ的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2021-12-12
  • C#中调用SAPI实现语音合成的2种方法

    C#中调用SAPI实现语音合成的2种方法

    这篇文章主要介绍了C#中调用SAPI实现语音合成的2种方法,本文直接给出示例代码,需要的朋友可以参考下
    2015-06-06
  • 基于静态Singleton模式的使用介绍

    基于静态Singleton模式的使用介绍

    本篇文章小编为大家介绍,基于静态Singleton模式的使用介绍。需要的朋友参考下
    2013-04-04
  • C#实现折半查找算法

    C#实现折半查找算法

    这篇文章介绍了C#实现折半查找的算法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-08-08
  • unity实现场景切换进度条显示

    unity实现场景切换进度条显示

    这篇文章主要为大家详细介绍了unity实现场景切换进度条显示,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-11-11
  • C#实现简单的字符串加密

    C#实现简单的字符串加密

    这篇文章介绍了C#实现字符串加密的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-06-06
  • C# 系统热键注册实现代码

    C# 系统热键注册实现代码

    简单点说就是为程序制定快捷键勒。。很多软件都带热键功能的,通过以下方式可以实现2个键或3个键的快捷键,相当之使用,具体实现方法看后文吧。
    2009-02-02

最新评论