最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#中foreach實(shí)現(xiàn)原理詳解

 更新時間:2017年09月27日 09:22:09   作者:阿瓏  
這篇文章主要為大家詳細(xì)介紹了C#中foreach實(shí)現(xiàn)原理,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文主要記錄我在學(xué)習(xí)C#中foreach遍歷原理的心得體會。

對集合中的要素進(jìn)行遍歷是所有編碼中經(jīng)常涉及到的操作,因此大部分編程語言都把此過程寫進(jìn)了語法中,比如C#中的foreach。經(jīng)常會看到下面的遍歷代碼:

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

實(shí)際此代碼的執(zhí)行過程:

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

會發(fā)現(xiàn)有GetEnumerator()方法和IEnumerator<string>類型,這就涉及到可枚舉類型和枚舉器的概念。

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

// 摘要:
//   公開枚舉器,該枚舉器支持在非泛型集合上進(jìn)行簡單迭代。
  public interface IEnumerable
  {
    // 摘要:
    //   返回一個循環(huán)訪問集合的枚舉器。
    //
    // 返回結(jié)果:
    //   可用于循環(huán)訪問集合的 System.Collections.IEnumerator 對象。
    IEnumerator GetEnumerator();
  }

實(shí)現(xiàn)了此接口的類稱為可枚舉類型,是可以用foreach進(jìn)行遍歷的標(biāo)志。

方法GetEnumerator()的返回值是枚舉器,可以理解為游標(biāo)。

// 摘要:
//   支持對非泛型集合的簡單迭代。
  public interface IEnumerator
  {
    // 摘要:
    //   獲取集合中的當(dāng)前元素。
    //
    // 返回結(jié)果:
    //   集合中的當(dāng)前元素。
    //
    // 異常:
    //  System.InvalidOperationException:
    //   枚舉數(shù)定位在該集合的第一個元素之前或最后一個元素之后。
    object Current { get; }

    // 摘要:
    //   將枚舉數(shù)推進(jìn)到集合的下一個元素。
    //
    // 返回結(jié)果:
    //   如果枚舉數(shù)成功地推進(jìn)到下一個元素,則為 true;如果枚舉數(shù)越過集合的結(jié)尾,則為 false。
    //
    // 異常:
    //  System.InvalidOperationException:
    //   在創(chuàng)建了枚舉數(shù)后集合被修改了。
    bool MoveNext();
    //
    // 摘要:
    //   將枚舉數(shù)設(shè)置為其初始位置,該位置位于集合中第一個元素之前。
    //
    // 異常:
    //  System.InvalidOperationException:
    //   在創(chuàng)建了枚舉數(shù)后集合被修改了。
    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這個關(guān)鍵字以后,我們可以通過這樣的方式來創(chuàng)建枚舉器:

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);
  }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C#實(shí)現(xiàn)PDF簽名時添加時間戳的2種方法(附VB.NET代碼)

    C#實(shí)現(xiàn)PDF簽名時添加時間戳的2種方法(附VB.NET代碼)

    在PDF添加簽名時,支持添加可信時間戳來保證文檔的法律效應(yīng)。本文,將通過C#程序代碼介紹如何添加可信時間戳,可通過2種方法來實(shí)現(xiàn)。感興趣的可以了解一下
    2021-05-05
  • c#操作json示例分享

    c#操作json示例分享

    這篇文章主要介紹了c#操作json示例,需要的朋友可以參考下
    2014-03-03
  • C#常見算法面試題小結(jié)

    C#常見算法面試題小結(jié)

    這篇文章主要介紹了C#常見算法面試題,包含了常見的排序、字符串操作、類的操作等技巧,需要的朋友可以參考下
    2014-09-09
  • C#虛函數(shù)用法實(shí)例分析

    C#虛函數(shù)用法實(shí)例分析

    這篇文章主要介紹了C#虛函數(shù)用法,實(shí)例分析了C#中虛函數(shù)的功能與基本使用技巧,需要的朋友可以參考下
    2015-07-07
  • WPF中窗體最大化問題的解決方法

    WPF中窗體最大化問題的解決方法

    這篇文章主要給大家介紹了關(guān)于WPF中窗體最大化問題的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用wpf具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-10-10
  • Unity實(shí)現(xiàn)旋轉(zhuǎn)扭曲圖像特效

    Unity實(shí)現(xiàn)旋轉(zhuǎn)扭曲圖像特效

    這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)旋轉(zhuǎn)扭曲圖像特效,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-02-02
  • C#列出當(dāng)前系統(tǒng)所有正在運(yùn)行程序的方法

    C#列出當(dāng)前系統(tǒng)所有正在運(yùn)行程序的方法

    這篇文章主要介紹了C#列出當(dāng)前系統(tǒng)所有正在運(yùn)行程序的方法,涉及C#操作系統(tǒng)進(jìn)程的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-04-04
  • C# winform自定義翻頁控件詳解

    C# winform自定義翻頁控件詳解

    這篇文章主要為大家詳細(xì)介紹了C# winform自定義翻頁控件的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • 關(guān)于C#程序優(yōu)化的五十種方法

    關(guān)于C#程序優(yōu)化的五十種方法

    這篇文章主要介紹了C#程序優(yōu)化的五十個需要注意的地方,使用c#開發(fā)的朋友可以看下
    2013-09-09
  • C#并發(fā)編程之Task類詳解

    C#并發(fā)編程之Task類詳解

    Task是建立在線程池之上的一種多線程技術(shù),它的出現(xiàn)使Thread成為歷史。其使用方法非常簡單,本文就來通過幾個示例為大家講講它的具體使用吧
    2023-03-03

最新評論

舟山市| 张家川| 玉树县| 金堂县| 济阳县| 稷山县| 浙江省| 宣威市| 西安市| 双江| 疏勒县| 深泽县| 鹤庆县| 托里县| 甘孜县| 乡城县| 佛山市| 理塘县| 方城县| 托克托县| 三明市| 扶沟县| 余干县| 浪卡子县| 宁津县| 五峰| 普兰县| 百色市| 铜川市| 玛沁县| 太康县| 泊头市| 明溪县| 尼勒克县| 商洛市| 汤阴县| 哈巴河县| 扎兰屯市| 将乐县| 祥云县| 五台县|