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

余姚什么网站做装修比较好邵阳公司网站建设

余姚什么网站做装修比较好,邵阳公司网站建设,怎么做提取微信62的网站,长沙seo优化排名推广一.迭代器 学过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.tj-hxxt.cn/news/132595.html

相关文章:

  • 网站设计与开发未来发展方向做食物网站应该考虑些什么
  • 当当网站建设与易趣网站对比wordpress企业免费主题下载地址
  • 影视公司网站设计银行营销活动方案
  • 电商网站建设课设上海建筑设计院工资
  • 网站找不到首页长春财经学院占地面积
  • .net网站开发岗位制作一个静态网页
  • 电商网站建设与管理建设部门三类人员官方网站
  • 做外汇看新闻在什么网站看app下载官方免费下载
  • 新浪网页版入口石家庄seo网络推广
  • 手机页面网站开发例子做直播导航网站好
  • 制作网站题材公司网站被百度收录
  • 克拉玛依做网站如何制作网站网页
  • 上海专业网站建站公用wordpress建一个网站
  • 江苏建设一体化平台网站网站构架怎么做
  • 互联网建设企业网站金融商城快捷申请网站模板下载
  • 创业如何进行网站建设wordpress每篇文章怎么加关键词
  • 常熟高端网站建设wordpress 多站
  • html5网站建设源码wordpress没有upload
  • 山西两学一做网站登录网站ui设计师培训
  • 一个旅游网站建设网站搭建推广优化
  • 学校自己做的网站需要买服务器吗注册小规模公司流程以及费用
  • 广东建设网 工程信息网站可以做推广东西的网站
  • 上市公司网站建设方案小程序一个页面多少钱
  • 造作网站模版网站设计手机版为什么那么多背景
  • 网站模板 登陆个人品牌网站建设
  • 常用网站建设技术是什么意思自动生成代码的软件
  • 网站开发需要做什么怎样做网站链接
  • 中小企业网站建设与管理课后答案深圳横岗做网站的
  • 起重机网站怎么做有什么公司是建设网站的吗
  • 绵阳新农网的网站是哪个公司做的石狮网站建设科技