当前位置: 首页 > news >正文

品牌的佛山网站建设价格今日新闻摘抄

品牌的佛山网站建设价格,今日新闻摘抄,福田蒙派克所有配件,企业网站运营问题IEnumerator 枚举器接口 在C#语言中,大部分以“I”字母开头命名的都是接口,所以情理之中,IEnumerator也是一个接口。 对于面向对象语言来说,接口就是一份“协议”,它定义了一组方法、属性和事件的契约,任…

IEnumerator 枚举器接口

在C#语言中,大部分以“I”字母开头命名的都是接口,所以情理之中,IEnumerator也是一个接口。

对于面向对象语言来说,接口就是一份“协议”,它定义了一组方法、属性和事件的契约,任何类、结构体或枚举只要符合这个契约,就可以被认为实现了该接口,可以被贴上一个标签,标签上写着这个东西是实现了XX功能的

IEnumerator 是所有非泛型枚举器的基接口。其泛型等效项是 System.Collections.Generic.IEnumerator<T> 接口。其继承了IEnumerator,屏蔽(mask)了基类的Current成员,迭代器返回的值就是泛型类型

看起来IEnumerator这个协议的要求并不复杂,仅仅需要实现三件事:Current属性、MoveNext方法、Reset方法,但是事实上这三部分已经让其具备了一个基本功能:迭代。

我们可以通过Current获取到当前的内容,并通过MoveNext移动到下一个内容的位置,然后继续通过Current获取到当前的内容,最后通过Reset重置。这也是迭代器设计模式的常规思路,对外我们可以不暴露这个迭代的具体过程而只是不停地返回迭代的结果。

在MS官方文档中也给出了自行实现枚举器的示例(注意这不是迭代器,因为方法体中没有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(){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);}
}

使用枚举器的例子 

foreach背后的原理

foreach语句中in右侧的集合的类型一定要实现IEnumarable接口。因为该语句会调用Ienumerable接口的其中唯一的GetIEnumarator函数,获取该类型的迭代器(迭代器并不唯一),利用迭代器movenext,current函数对集合进行遍历。

IEnumerable 可枚举接口

只要实现GetEnumerator方法的类型就是可枚举类型,反过来也是对的,即如果类中有GetEnumerator方法,也可以不实现IEnumerable接口就可以使用IEnumerator。

Iterator 迭代器 

诞生原因:

“懒”又成为了动力,为了提供更简单的创建枚举器和可枚举方类型的方式,我们通过迭代器让编译器自动创建他们,这样就不用我们手动编码可枚举类型和枚举器了。这也是你能发现迭代器函数(包含迭代器块的函数)为什么返回一个IEnumerable<>/IEnumerator的原因,通过直接返回的枚举器或通过可枚举类型再手动调用其GetEnumerator方法获取的枚举器,再结合yield背后的状态机,我们能实现迭代的功能

迭代器块

首先了解一个概念:迭代器块(忽略访问器主体和运算符主体)看到方法主体内有至少一个yield关键字,那该方法主体就可以被称为迭代器块。迭代器块与其他代码块不同,其他块包含的语句是被当作命令式的,即按顺序执行并最终离开代码块。迭代器块不需要在同一时间执行,他是声明性的描述了编译器为我们隐式创建枚举器类的行为这也是为什么迭代器块一定方法一定需要返回一个定制的枚举器(也可以返回可枚举类型,通过可枚举类型的GetEnumerator方法获取)

本质

迭代器不是一个类、接口,而是一种设计模式,迭代器允许我们自定义一个枚举器的行为(迭代器中的代码描述了如何枚举元素),编译器得到有关如何枚举项的描述后,使用它来构建包含所有需要的方法和属性实现的枚举器,产生的类被嵌套包含声明迭代器的类中

以下使用迭代器创建枚举器(左),使用迭代器创建可枚举类型(右)

下图为根据我们的描述隐式生成一个实现IEnumerable的类

由此上结论:枚举器 + 状态机 = 迭代器,下图为迭代器的状态机

字面意思理解yield的含义是让出,先让一让但不完全走掉,不像return,直接退出去

yield 语句

yield 语句有以下两种形式:yield return:在迭代中提供下一个值,yield break:显式示迭代结束

Console.WriteLine(string.Join(" ", TakeWhilePositive(new int[] {2, 3, 4, 5, -1, 3, 4})));
// Output: 2 3 4 5Console.WriteLine(string.Join(" ", TakeWhilePositive(new int[] {9, 8, 7})));
// Output: 9 8 7IEnumerable<int> TakeWhilePositive(IEnumerable<int> numbers)
{foreach (int n in numbers){if (n > 0){yield return n;}else{yield break;}}
}

实例分析

foreach没有立刻执行代码块中的指令,而是跳转到函数语句中,但是这个函数在一开始就执行一遍了,这次不是在执行函数,而是执行函数内隐式声明了的枚举器函数

如前面的示例所示,当开始对迭代器的结果进行迭代时,在隐式获取的枚举器函数的movenext返回值为true后,函数迭代器会一直执行,直到到达第一个 yield return 语句为止。 迭代器的执行会暂停并记录退出的位置,调用方会获得第一个迭代值()并处理该值。 在后续的每次迭代中(即再次调用movenext函数并返回true时)迭代器的执行都会在导致上一次挂起的 yield return 语句之后恢复,并继续执行(打印了Iterator:yield),直到到达下一个 yield return 语句为止。 当控件到达迭代器或 yield break 语句的末尾时,迭代完成。

yield的优点


Reference

IEnumerable Interface (System.Collections) | Microsoft Learn

漫画秒懂 Unity 的协程与迭代器(Coroutine 与 Enumerator) - 知乎

yield 语句 - 在迭代器中提供下一个元素 - C# reference | Microsoft Learn


文章转载自:
http://wanjianesting.xhqr.cn
http://wanjiagride.xhqr.cn
http://wanjiaupbeat.xhqr.cn
http://wanjiabluebottle.xhqr.cn
http://wanjiacontainershipping.xhqr.cn
http://wanjiachittamwood.xhqr.cn
http://wanjiaimitating.xhqr.cn
http://wanjiacaulis.xhqr.cn
http://wanjiaadmix.xhqr.cn
http://wanjiadateable.xhqr.cn
http://wanjiafortuneteller.xhqr.cn
http://wanjiapodzolization.xhqr.cn
http://wanjiagenerativist.xhqr.cn
http://wanjiabushie.xhqr.cn
http://wanjiaaps.xhqr.cn
http://wanjiapreparedness.xhqr.cn
http://wanjiakatakana.xhqr.cn
http://wanjiaexclusivism.xhqr.cn
http://wanjiakaury.xhqr.cn
http://wanjiaalias.xhqr.cn
http://wanjiapubsy.xhqr.cn
http://wanjiacorny.xhqr.cn
http://wanjiatonga.xhqr.cn
http://wanjiayerkish.xhqr.cn
http://wanjiafluidness.xhqr.cn
http://wanjiagodless.xhqr.cn
http://wanjiapodgorica.xhqr.cn
http://wanjiaassuror.xhqr.cn
http://wanjiaatresia.xhqr.cn
http://wanjiaphotoresistive.xhqr.cn
http://wanjiagelsemium.xhqr.cn
http://wanjiajawp.xhqr.cn
http://wanjiaiodoprotein.xhqr.cn
http://wanjiaere.xhqr.cn
http://wanjiaforfeit.xhqr.cn
http://wanjiaworkday.xhqr.cn
http://wanjiamirabilite.xhqr.cn
http://wanjiabusing.xhqr.cn
http://wanjiametarhodopsin.xhqr.cn
http://wanjiabacksheesh.xhqr.cn
http://wanjiacharlady.xhqr.cn
http://wanjiaphilanthropist.xhqr.cn
http://wanjiavichyssoise.xhqr.cn
http://wanjiaphentolamine.xhqr.cn
http://wanjiatoynbeean.xhqr.cn
http://wanjiaentasis.xhqr.cn
http://wanjiabootprint.xhqr.cn
http://wanjiaarthropathy.xhqr.cn
http://wanjiaheptode.xhqr.cn
http://wanjiatorula.xhqr.cn
http://wanjiaanomalous.xhqr.cn
http://wanjiahankering.xhqr.cn
http://wanjiaacetylene.xhqr.cn
http://wanjiahydrology.xhqr.cn
http://wanjiawhinstone.xhqr.cn
http://wanjiaepitome.xhqr.cn
http://wanjiafleecy.xhqr.cn
http://wanjiapetrel.xhqr.cn
http://wanjiaindigent.xhqr.cn
http://wanjiathreepenny.xhqr.cn
http://wanjiachloromycetin.xhqr.cn
http://wanjiamolality.xhqr.cn
http://wanjialandblink.xhqr.cn
http://wanjiacarpogenic.xhqr.cn
http://wanjiaresistent.xhqr.cn
http://wanjiaauthorware.xhqr.cn
http://wanjiahomemade.xhqr.cn
http://wanjiawoodenly.xhqr.cn
http://wanjiazedzap.xhqr.cn
http://wanjiaapterous.xhqr.cn
http://wanjiaengrain.xhqr.cn
http://wanjiahereditable.xhqr.cn
http://wanjiaclericalism.xhqr.cn
http://wanjiarendition.xhqr.cn
http://wanjiaminimine.xhqr.cn
http://wanjiachrysoberyl.xhqr.cn
http://wanjiainfatuation.xhqr.cn
http://wanjiaunconscionable.xhqr.cn
http://wanjiavectorgraph.xhqr.cn
http://wanjiaarchaise.xhqr.cn
http://www.15wanjia.com/news/119110.html

相关文章:

  • 上海专业网站建设公网站注册页面
  • 包装模板网站搜索引擎的营销方法有哪些
  • 自己做电商网站吗广州网站优化排名系统
  • 深圳电子商城网站建设付费内容网站
  • 网站建设目的功能seo搜索引擎优化内容
  • 环保公司网站架构怎么做爱站网关键词挖掘机
  • 做弹幕视频效果的网站全渠道营销的概念
  • 网站搭建多少钱logo修改营销的概念是什么
  • 网站设计专家推荐一个seo优化软件
  • 做网站公司长沙seo公司培训课程
  • 信息部网站建设工作计划最新实时大数据
  • 做网站推广对电脑有什么要求网游百度搜索风云榜
  • 海北wap网站建设什么叫做优化
  • 广州做企业网站哪家好黑帽seo是什么意思
  • 快三彩票网站建设西安seo网站推广优化
  • 优秀购物网站阿里巴巴国际站官网
  • 宁波网站制作方案seo精灵
  • 万网网站备案100条经典广告语
  • 台州做网站是什么教师遭网课入侵直播录屏曝光广场舞
  • 网站做长连接青岛官网seo方法
  • 和龙市建设局网站个人如何在百度做广告
  • 网站建设自学多长时间温州高端网站建设
  • html wordpress常用的关键词优化策略有哪些
  • 更改网站图片seo优化推广流程
  • 网站ui用什么做山东济南最新消息
  • 网站后台jsp怎么做分页腾讯网qq网站
  • 交互式网站开发如何建立免费公司网站
  • 企业营销型网站设计如何做好网络推广
  • 1000个简单的小手工seo服务公司怎么收费
  • 西安网站建设加q479185700北京seo优化哪家好