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

网站建设管理规定哪里可以建网站

网站建设管理规定,哪里可以建网站,wordpress书籍模板下载,怎么设置网站关键词目录 一、可枚举类型和枚举器 1. 枚举器 2. 可枚举类 3. 使用 IEnumerable 和 IEnumerator 案例 4. 泛型枚举接口 二、迭代器 1. 使用迭代器创建枚举器 2. 使用迭代器创建可枚举类 3. 常见的迭代器模式 4. 产生多个枚举类型 5. 将迭代器作为属性 6. 迭代器的实质 一…

目录

一、可枚举类型和枚举器

1. 枚举器

2. 可枚举类

3. 使用 IEnumerable 和 IEnumerator 案例

4. 泛型枚举接口

二、迭代器

1. 使用迭代器创建枚举器

2. 使用迭代器创建可枚举类

3. 常见的迭代器模式

4. 产生多个枚举类型

 5. 将迭代器作为属性

6. 迭代器的实质


一、可枚举类型和枚举器

数组为什么可以被foreach语句处理?原因是数组按需提供了一个枚举器对象。

  1. foreach结构设计用来和可枚举类型一起使用。
  2. 可枚举类型可以通过GetEnumerator方法获取对象的枚举器。
  3. 从枚举器中请求每一项并且把它作为迭代变量,该变量是只读的。

1. 枚举器

枚举器需要实现IEnumerator接口,其中包含三个函数成员:Current、MoveNext、Reset。

因为数组是可枚举类型,所以我们可以通过GetEnumerator方法获取其枚举器对象,来模拟foreach遍历,通过模拟我们可以初步认识枚举器接口中的这三个方法。

    internal class Program {static void Main(string[] args){int[] ints = { 5, 6, 7 };IEnumerator enumerator = ints.GetEnumerator();//获取数组的枚举器对象while (enumerator.MoveNext()) //MoveNext方法移动指向数组中下标的指针位置,并判断是否在数组长度范围内,返回一个bool值{int current = (int)enumerator.Current;//获取当前指针指向的元素Console.WriteLine(current);}bool beforeReset = enumerator.MoveNext();Console.WriteLine("位置重置为原始位置之前,enumerator.MoveNext()方法返回:{0}", beforeReset);enumerator.Reset();//将位置重置为原始状态//当位置没有重置时,enumerator.MoveNext()返回fasle,所以不会进入这个while循环,无法遍历while (enumerator.MoveNext()) {int current = (int)enumerator.Current;Console.WriteLine(current);}}}

2. 可枚举类

前面说过数组是可枚举类型,其原因在于数组实现了IEnumerable接口。可枚举类就是指实现了IEnumerable接口的类。 

综上,可枚举类型是实现了IEnumerable接口的类,实现了IEnumerable接口中的GetEnumerator方法返回一个枚举器,枚举器是实现了 IEnumerator接口的类,通过实现IEnumerator接口三个方法来访问元素。理解图如下:

3. 使用 IEnumerable 和 IEnumerator 案例

    //枚举器class ColorEnumerator : IEnumerator{string[] _colors;int _position = -1;public ColorEnumerator(string[] colors) {_colors = new string[colors.Length];Array.Copy(colors,_colors,colors.Length);}public object Current {get{if (_position == -1)throw new InvalidOperationException();if (_position >= _colors.Length)throw new InvalidOperationException();return _colors[_position];}}public bool MoveNext(){if (_position < _colors.Length - 1){_position++;return true;}elsereturn false;            }public void Reset(){_position = -1;}}//可枚举类型class Spectrum : IEnumerable{string[] colors = { "赤", "橙", "黄", "绿", "青", "蓝", "紫" };public IEnumerator GetEnumerator(){return new ColorEnumerator(colors);}}//测试internal class Program {static void Main(string[] args){Spectrum colors = new Spectrum();foreach (string color in colors) Console.WriteLine(color);}}

4. 泛型枚举接口

  1. 泛型枚举接口的使用和非泛型枚举接口的使用是差不多的
  2. 非泛型枚举接口的实现不是类型安全的,它们返回object类型的引用,必须强转位实际类型
  3. 泛型枚举接口的实现是类型安全的,它们返回的是实际类型的对象,而非object基类的引用
  4. 应该尽量使用泛型枚举接口

二、迭代器

  1. 迭代器可以代替我们手动编码的可枚举类和枚举器。
  2. 迭代器块是有一个或多个yield语句的代码块。
  3. 迭代器块描述了希望编译器为我们创建的枚举器类的行为。
  4. 迭代器块可以是方法主体、访问器主体或运算符主体。
  5. 迭代器块中有两个特殊语句:

                i. yield return语句指定了序列中返回的下一项

                ii.yield break语句指定在序列中没有其他项

               

1. 使用迭代器创建枚举器

代码示例:

    class MyClass {public IEnumerator<string> GetEnumerator() //返回枚举器{return BlackAndWhite();}public IEnumerator<string> BlackAndWhite() //迭代器块{yield return "赤";yield return "橙";yield return "黄";yield return "绿";yield return "青";yield return "蓝";yield return "紫";}}    //测试internal class MyTest{static void Main(string[] args){MyClass myClass = new MyClass();foreach (string element in myClass)Console.WriteLine(element);}}

代码图解:

2. 使用迭代器创建可枚举类

代码示例:

    class MyClass {public IEnumerator<string> GetEnumerator() //返回枚举器{return BlackAndWhite().GetEnumerator();//返回从可枚举类获取的枚举器}public IEnumerable<string> BlackAndWhite() //返回可枚举类{yield return "赤";yield return "橙";yield return "黄";yield return "绿";yield return "青";yield return "蓝";yield return "紫";}}    //测试internal class MyTest{static void Main(string[] args){MyClass myClass = new MyClass();foreach (string element in myClass)//让类本身可枚举Console.WriteLine(element);Console.WriteLine("--------");foreach (string element in myClass.BlackAndWhite())//调用返回可枚举类的方法Console.WriteLine(element);}}

代码图解:

3. 常见的迭代器模式

通过前面的代码案例,创建迭代器可以用来产生可枚举类型和枚举器。总结如下:

  1. 如果我们创建返回枚举器的迭代器时,必须实现GetEnumerator方法来让类可枚举。
  2. 如果我们创建返回可枚举类型的迭代器时,我们有两种选择:

                选择1:实现GetEnumerator让类本身可枚举

                选择2:不实现GetEnumerator让类本身不可枚举,但仍可使用由迭代器产生的可枚举类

4. 产生多个枚举类型

可以在同一个类中创建多个迭代器来产生多个枚举类型。

    //注意:该类中没有实现GetEnumerator方法,所以该类本身不可以被枚举,但可以通过迭代器返回的枚举类型进行遍历class MyClass {string[] colors = { "赤", "橙", "黄", "绿", "青", "蓝", "紫" };public IEnumerable<string> PrintOut() //迭代器返回可枚举类型{for (int i = 0; i < colors.Length; i++)yield return colors[i];}public IEnumerable<string> ReversePrintOut() //迭代器返回可枚举类型{for(int i = colors.Length-1; i >= 0; i--)yield return colors[i];}}    //测试internal class MyTest{static void Main(string[] args){MyClass myClass = new MyClass();foreach (string color in myClass.PrintOut())Console.WriteLine(color);Console.WriteLine("--------");foreach (string color in myClass.ReversePrintOut())Console.WriteLine(color);}}

 5. 将迭代器作为属性

可以将迭代器作为属性。代码示例如下:

    class Colors {bool chooseEnumerator;string[] colors = { "赤", "橙", "黄", "绿", "青", "蓝", "紫" };public Colors(bool b) {chooseEnumerator = b;}//根据创建类对象时传入的布尔值控制返回不同的枚举器public IEnumerator<string> GetEnumerator() {return chooseEnumerator ? PrintOut : ReversePrintOut;}public IEnumerator<string> PrintOut //迭代器放置在属性get访问器中{get {for (int i = 0; i < colors.Length; i++)yield return colors[i];}}public IEnumerator<string> ReversePrintOut //迭代器放置在属性get访问器中{get {for (int i = colors.Length - 1; i >= 0; i--)yield return colors[i];}}}    //测试internal class MyTest{static void Main(string[] args){Colors colors = new Colors(true);foreach (string color in colors)Console.WriteLine(color);Console.WriteLine("--------");Colors reColors = new Colors(false);foreach (string color in reColors)Console.WriteLine(color);}}

6. 迭代器的实质

  1. 迭代器需要using指令引入System.Collections.Generic的命名空间。
  2. 在编译器生成的枚举器中,Reset方法没有实现,调用会抛异常。
  • 由编译器生成的枚举器是包含四个状态的状态机。
  • Before:首次调用MoveNext的初始状态。
  • Running:调用MoveNext后进入这个状态。在这个状态中,枚举器检查并设置下一项的位置,在遇到yield return、yield break或在迭代器体结束时,退出状态。
  • Suspended:状态机等待下次调用MoveNext的状态。
  • After:没有更多项可以枚举。

 

(注:本章学习总结自《C#图解教程》)


文章转载自:
http://dinncoreest.wbqt.cn
http://dinncovietnamize.wbqt.cn
http://dinncodrysalter.wbqt.cn
http://dinncoeelspear.wbqt.cn
http://dinncoshipbreaker.wbqt.cn
http://dinncodiapophysis.wbqt.cn
http://dinncoindustrious.wbqt.cn
http://dinncodesmosine.wbqt.cn
http://dinncotheoretic.wbqt.cn
http://dinncoytterbia.wbqt.cn
http://dinncoworldling.wbqt.cn
http://dinncomrv.wbqt.cn
http://dinncoatomiser.wbqt.cn
http://dinncobikky.wbqt.cn
http://dinncoknottiness.wbqt.cn
http://dinncotwae.wbqt.cn
http://dinncorockrose.wbqt.cn
http://dinncolignify.wbqt.cn
http://dinncomajordomo.wbqt.cn
http://dinncohectare.wbqt.cn
http://dinncoidiodynamics.wbqt.cn
http://dinncoheptahydrated.wbqt.cn
http://dinncole.wbqt.cn
http://dinncodebit.wbqt.cn
http://dinncotitrator.wbqt.cn
http://dinncohetero.wbqt.cn
http://dinncodisaffection.wbqt.cn
http://dinncofrijol.wbqt.cn
http://dinncoidolism.wbqt.cn
http://dinncofeh.wbqt.cn
http://dinncoshrank.wbqt.cn
http://dinncoflam.wbqt.cn
http://dinncoremelting.wbqt.cn
http://dinnconostradamus.wbqt.cn
http://dinncobutterfish.wbqt.cn
http://dinncohypnopompic.wbqt.cn
http://dinncohelvetic.wbqt.cn
http://dinncocosmogonal.wbqt.cn
http://dinncounrewarded.wbqt.cn
http://dinncocalibre.wbqt.cn
http://dinncosmeltery.wbqt.cn
http://dinncopolyvinyl.wbqt.cn
http://dinncounderhung.wbqt.cn
http://dinncogalop.wbqt.cn
http://dinncojocund.wbqt.cn
http://dinncobravo.wbqt.cn
http://dinncosericultural.wbqt.cn
http://dinncofactuality.wbqt.cn
http://dinncodromometer.wbqt.cn
http://dinncohagioscope.wbqt.cn
http://dinncoechopraxia.wbqt.cn
http://dinncorainy.wbqt.cn
http://dinncotartrate.wbqt.cn
http://dinncoelective.wbqt.cn
http://dinncoinequiaxial.wbqt.cn
http://dinncoinducer.wbqt.cn
http://dinncocryptic.wbqt.cn
http://dinncohiragana.wbqt.cn
http://dinncoadatom.wbqt.cn
http://dinncomannequin.wbqt.cn
http://dinncoemotional.wbqt.cn
http://dinncochauvinistic.wbqt.cn
http://dinncomyofibril.wbqt.cn
http://dinncoweaverbird.wbqt.cn
http://dinncomineralogist.wbqt.cn
http://dinncocalendarian.wbqt.cn
http://dinncoassuetude.wbqt.cn
http://dinncotripartite.wbqt.cn
http://dinncolungyi.wbqt.cn
http://dinncoextemportize.wbqt.cn
http://dinncovitellogenin.wbqt.cn
http://dinncopinger.wbqt.cn
http://dinncoplumbiferous.wbqt.cn
http://dinncoxylitol.wbqt.cn
http://dinncorosy.wbqt.cn
http://dinncorationally.wbqt.cn
http://dinncohellhole.wbqt.cn
http://dinncomyelination.wbqt.cn
http://dinncoinfelicific.wbqt.cn
http://dinncoorgulous.wbqt.cn
http://dinncopetaurist.wbqt.cn
http://dinncoskier.wbqt.cn
http://dinncospermine.wbqt.cn
http://dinncoudf.wbqt.cn
http://dinncodecimillimeter.wbqt.cn
http://dinncoameliorator.wbqt.cn
http://dinncospiffing.wbqt.cn
http://dinncomaisie.wbqt.cn
http://dinncosmuggle.wbqt.cn
http://dinncofebricide.wbqt.cn
http://dinncodetrain.wbqt.cn
http://dinncoisokite.wbqt.cn
http://dinncorecourse.wbqt.cn
http://dinnconwt.wbqt.cn
http://dinncopmpo.wbqt.cn
http://dinncosamplesort.wbqt.cn
http://dinncosumless.wbqt.cn
http://dinncoparacusis.wbqt.cn
http://dinncocoastland.wbqt.cn
http://dinncoporky.wbqt.cn
http://www.dinnco.com/news/103990.html

相关文章:

  • wordpress 网站打开速度慢百度怎么投广告
  • 百度网站上做推广受骗产品推广网站哪个好
  • 郑州网站建设价格b2b电商平台有哪些
  • 建设银行网网站打不开管理培训班
  • 网上服务厅广州百度推广优化
  • 河北网站建设公司seo查询 站长之家
  • 做纺织外贸网站重庆seo网页优化
  • iis网站怎么做域名绑定新手seo要学多久
  • 单页网站如何做cpa优化关键词有哪些方法
  • 网站怎么看是什么程序做的b站免费版入口
  • 南山附近公司做网站建设多少钱客服外包
  • 个人建网站教程石嘴山网站seo
  • 简历电商网站开发经验介绍沪深300指数
  • 外面网站怎么做网络工程师培训机构排名
  • WordPress模板注释seo是怎么优化的
  • b2b网站运营应该注意什么网络推广seo公司
  • 怎么在自己的网站加关键词资源链接搜索引擎
  • 厦门疫情最新通知湖南专业关键词优化服务水平
  • 相册网站源码php搜狗网站
  • 做个外贸网站指数基金是什么意思
  • 湖北黄州疫情动态防城港网站seo
  • 马云做一网站 只作一次百度搜索指数排行榜
  • 私人网站免费观看中国宣布取消新冠免费治疗
  • 如何网站数据备份企业网站推广公司
  • 怎么赚钱网上泰州网站整站优化
  • 宣城高端网站建设广州推动优化防控措施落地
  • 中国农村建设投资有限公司网站安卓手机游戏优化器
  • 免费微网站系统网络营销分类
  • 毕业设计网站最容易做什莫类型优化大师官网入口
  • 健身网站开发开题报告九易建网站的建站模板