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

菏泽建设局网站做网站要求什么

菏泽建设局网站,做网站要求什么,wordpress制作分类层级,apicloud成品源码一.迭代器 学过C的都知道#xff0c;迭代器是STL中的一个类#xff0c;用来遍历容器。 vectorint::iterator iter; for (iter v1.begin(); iter ! v1.end(); iter) C#中迭代器也是用来遍历集合#xff0c;本质上是一个类/接口(IEnumerator)#xff0c;可以解决…一.迭代器 学过C的都知道迭代器是STL中的一个类用来遍历容器。 vectorint::iterator iter; for (iter v1.begin(); iter ! v1.end(); iter) C#中迭代器也是用来遍历集合本质上是一个类/接口(IEnumerator)可以解决在不知道集合以何种key存储的情况下对集合进行遍历。 int[] nums {1, 2, 3, 4, 5}; foreach(int i in nums) {Console.WriteLine(i); } 可以看到C#的迭代器i没有初始值结束条件变化的情况这是因为C#中的foreach隐藏了迭代器的复杂性foreach是一种语法糖。可以用foreach遍历的集合必须实现接口IEnumerableIEnumerator. 二.IEnumerator接口 public interface IEnumerator {bool MoveNext();object Current { get; }void Reset(); } MoveNext将当前元素向前移动到下一个位置如果集合没有更多元素那么它会返回false。Current返回当前位置元素。在取得第一个元素之前 必须先调用MoveNext即使空集合也支持该操作。Reset作用就是当前位置移回起点并允许再一次枚举集合。此方法一般并不建议使用因为完全可以重新实例化一个枚举器。 三.IEnumerable接口 public interface IEnumerable: {IEnumerator GetEnumerator(); } 通过GetEnumerator返回枚举器IEnumerable可以看作IEnumerator的提供者。 下面例子演示foreach是一种语法糖本质是调用接口IEnumerator的MoveNext方法和Current属性 class MainClass{static void Main(){string s Hello;IEnumeratorchar iter s.GetEnumerator(); //通过GetEnumerator方法获取迭代器while (iter.MoveNext()) //通过MoveNext方法判断是否还有下一个元素并向后移动{char c (char)iter.Current; //通过Current属性获取元素Console.Write(c _);}Console.WriteLine();foreach(char c in s){Console.Write(c .);}Console.WriteLine();}}/* Output:H_e_l_l_oH.e.l.l.o*/ 四.遍历自定义集合的做法 下面的代码示例演示自定义集合的IEnumerable和IEnumerator接口的实现。 在此示例中不会显式调用这些接口的成员它们实现为支持使用foreach循环访问集合。 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**/ 五.IEnumerable返回IEnumerator的作用 先说结论如果集合实现IEnumerator的话存在问题。 当出现循环嵌套时内循环跳出时会把index制为-1外循环永远无法结束造成死循环。 为了解决这一问题IEnumerable的GetEnumerator方法会返回一个new出来的IEnumerator对象
http://www.hkea.cn/news/14339124/

相关文章:

  • 常见cms网站源码下载深圳龙华属于哪个区
  • 三亚网站运营托管介绍广州站到广州南站地铁要多久
  • 网站建设方案范本找大学生做家教的网站
  • 运动网站源码鼓楼区建设局网站
  • 网站会员和discuz会员同步百度站长提交网站地图
  • 网站建设 wordpress系统怎么做微信小程序
  • 杭州网站建设公司费用郑州一建集团公司官网
  • 新建网站百度搜不到佛山旺道seo优化
  • 凯里做网站长宁广州网站建设
  • h5响应式网站建设报价做动画网站公司
  • 东莞南城网站制作天美大象果冻星空的制作方法
  • 触屏网站开发教程沈阳市和平区建设局网站
  • 有没有做盗版电影网站犯罪的php网站投票源码
  • 黔江网站建设wordpress 页面名称
  • 网站开发模板图片网站建站咨询
  • 网站建设系统分析温州网络学堂
  • 广州做购物网站乌兰察布网站制作
  • 九江网站建设张旭互联网推广营销隐迅推认定
  • 溧阳手机网站设计wordpress网站建设中
  • 如何选择丹徒网站建设m版网站开发
  • 什么是空壳网站出口贸易网站
  • 浙江网站备案流程nginx wordpress怎么样
  • 网站建设工作室+怎么样万网 速成网站
  • 音乐网站建设教程企业公司网站模版
  • 网站制作有限公司广南网站建设
  • 安卓app做网站外壳城市房产网
  • wordpress浮动留言板seo长尾关键词优化
  • 跨境网站建站wordpress 禁用评论
  • 石家庄无极网站建设沈阳市网站制作公司
  • 模板网站建设全过程网站 制作软件