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

网站模版怎么用广州seo工资

网站模版怎么用,广州seo工资,做网站工资怎么样,大连网站建设方案案例为什么数组和集合可以使用foreach遍历 因为数组和集合都实现了IEnumerable接口,该接口中只有一个方法,GetEnumerator()。 数组类型是从抽象基类型 Array 派生的引用类型。由于此类型实现了 IEnumerable ,因此可以对 C# 中的所有数组使用 foreach 迭代。…

为什么数组和集合可以使用foreach遍历

因为数组和集合都实现了IEnumerable接口,该接口中只有一个方法,GetEnumerator()。

数组类型是从抽象基类型 Array 派生的引用类型。由于此类型实现了 IEnumerable ,因此可以对 C# 中的所有数组使用 foreach 迭代。

IEnumerable和IEnumerator接口定义

    // 摘要://     公开枚举器,该枚举器支持在非泛型集合上进行简单迭代。public interface IEnumerable{// 摘要://     返回一个循环访问集合的枚举器。//// 返回结果://     可用于循环访问集合的 System.Collections.IEnumerator 对象。IEnumerator GetEnumerator();}public interface IEnumerable<out T> : IEnumerable{new IEnumerator<T> GetEnumerator();}// 摘要://     支持对非泛型集合的简单迭代。public interface IEnumerator{// 摘要://     获取集合中的当前元素。//// 返回结果://     集合中的当前元素。//// 异常://   System.InvalidOperationException://     枚举数定位在该集合的第一个元素之前或最后一个元素之后。object Current { get; }// 摘要://     将枚举数推进到集合的下一个元素。//// 返回结果://     如果枚举数成功地推进到下一个元素,则为 true;如果枚举数越过集合的结尾,则为 false。//// 异常://   System.InvalidOperationException://     在创建了枚举数后集合被修改了。bool MoveNext();//// 摘要://     将枚举数设置为其初始位置,该位置位于集合中第一个元素之前。//// 异常://   System.InvalidOperationException://     在创建了枚举数后集合被修改了。void Reset();}public interface IEnumerator<out T> : IDisposable, IEnumerator{    new T Current { get; }}

Enumerator实现

        [Serializable]public struct Enumerator : IEnumerator<T>, System.Collections.IEnumerator{private List<T> list;private int index;private int version;private T current;internal Enumerator(List<T> list) {this.list = list;index = 0;version = list._version;current = default(T);}public void Dispose() {}public bool MoveNext() {List<T> localList = list;if (version == localList._version && ((uint)index < (uint)localList._size)) {                                                     current = localList._items[index];                    index++;return true;}return MoveNextRare();}private bool MoveNextRare(){                if (version != list._version) {ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion);}index = list._size + 1;current = default(T);return false;                }public T Current {get {return current;}}Object System.Collections.IEnumerator.Current {get {if( index == 0 || index == list._size + 1) {ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumOpCantHappen);}return Current;}}void System.Collections.IEnumerator.Reset() {if (version != list._version) {ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion);}index = 0;current = default(T);}}

可以看到它实际上也是通过for循环去遍历的。

对于List集合.NET是怎么实现这个IEnumerable接口的

以泛型List为例,它继承了IList<T>接口,而IList<T>继承了ICollection<T>接口,ICollection<T>继承了IEnumerable<T>这个接口。

找到它的具体实现,实际上是返回了一个枚举器,通过此枚举器完成对列表元素的遍历。

IEnumerator<T> IEnumerable<T>.GetEnumerator() 
{return new Enumerator(this);
}
 

也就是说,实际上foreach的原型代码实际上可以写成

IEnumerator enumerator = list.GetEnumerator();
while(enumerator.MoveNext())
{Debug.Log(enumerator.Current);
}
 

如何自定义一个类型使它可以使用foreach去遍历

例如有这样一个类Person:

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

定义另一个类People来实现IEnumerable迭代器完成对Person的迭代,在返回枚举器时返回的是实现了IEnumerator的PeopleEnum类型实例:

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

实际应用时:

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

配合yield关键字可以通过更简易的方式来创建枚举器

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

yield和return一起使用才有意义,这个关键字就是为迭代器服务的,所以有yield所在的方法是迭代器。yield关键字其实是一种语法糖,最终还是通过实现IEnumberable<T>、IEnumberable、IEnumberator<T>和IEnumberator接口实现的迭代功能。

http://www.lbrq.cn/news/2592469.html

相关文章:

  • 信阳建设企业网站百度信息流广告平台
  • 网站图片太多怎么办怎么做网络营销推广
  • 做二手车的网站有哪些杭州余杭区抖音seo质量高
  • 沈阳微信网站开发推广软件是什么工作
  • 网站推广昔年下拉湘潭关键词优化公司
  • 什么软件做网站最好seo优化需要多少钱
  • 保定网站建设培训班深圳网络推广公司有哪些
  • 广南网站建设今日新闻头条最新消息
  • 知名网站建设怎么样域名查询网站入口
  • 关于网站建设的博客网络推广的方法
  • 泰安房产信息网官网湖南专业seo优化
  • 现工作室专做网站建设等应用程序项目,但工作室名暂为哪家网络公司比较好
  • 在网站上做承诺书拉新人拿奖励的app
  • 企业网站是怎么建站的南京seo新浪
  • 网站建设具体建设流程网站建设深圳公司
  • 做产品网站费用吗网络推广方案范文
  • 万网主机网站建设数据库怎么弄抖音推广佣金平台
  • wordpress 主机屋企业网站推广优化公司
  • 小企业网站建设怎么做好上线了建站
  • 外贸网站制作时间及费用百度竞价ocpc
  • 营养早餐网站的设计与制作免费推广网站2024
  • 为什么现在建设银行要下载网站激活搜索热门关键词
  • 手机网站 jquery 特效seo型网站
  • 在线网页代理太太猫奶糖 seo 博客
  • 阿里云服务器 多个网站关键词上首页的有效方法
  • 咖啡厅网站建设客服网站搭建
  • 农产品网站建设策划书范文百度舆情监测平台
  • 南昌哪家做网站好看b站视频软件下载安装手机
  • 企业做网站需要什么软件uc信息流广告投放
  • 长治个人做网站微信朋友圈广告投放
  • 【Java】使用FreeMarker来实现Word自定义导出
  • C++入门自学Day6-- C++模版
  • Apache IoTDB(3):时序数据库 IoTDB Docker部署实战
  • Claude Code深度操作指南:从零到专家的AI编程助手实战
  • JVM学习日记(十六)Day16——性能监控与调优(三)
  • Python 小数据池(Small Object Pool)详解