资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

IEnumerable接口与IEnumerator接口-创新互联

通过一个例子来看

在灯塔等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供做网站、网站设计 网站设计制作定制制作,公司网站建设,企业网站建设,品牌网站制作,成都营销网站建设,成都外贸网站建设,灯塔网站建设费用合理。

-------------------------------------------------------Student.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace ConsoleApplication6
{
    public class Student:IEnumerable
    {
        //数组
        public string[] s;
        //索引器
        public int i;
        public Student(string[] str)//构造函数,初始化数组
        {
            s = str;
        }
        public IEnumerator GetEnumerator()//迭代器
        {
            return s.GetEnumerator();
        }
    }
}

-------------------------------------------------------StiAll.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace ConsoleApplication6
{
    public class StiAll:IEnumerator
    {
        //student对象
        Student s;
        //游标
        int i = -1;
        public StiAll(Student ss)//构造函数,初始化student对象
        {
            this.s = ss;
        }
 
        public object Current//获取当前的项(只读属性)
        {
            get { return s.s[i]; }
        }
        public bool MoveNext()//将游标的位置向前移动
        {
            if (i

-------------------------------------------------------主程序

 Student s = new Student(new string[] { "吕蒙", "周泰", "黄盖" });//实例化Student对象
            //第一种方式遍历
            foreach (var item in s)
            {
                Console.WriteLine(item);//输出吕蒙,周泰,黄盖
            }
            //第二种方式遍历
            StiAll sa = new StiAll(s);
            while (sa.MoveNext())
            {
                Console.WriteLine(sa.Current);//输出吕蒙,周泰,黄盖
            }
            Console.ReadKey();

创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。


网页名称:IEnumerable接口与IEnumerator接口-创新互联
转载源于:http://cdkjz.cn/article/jdhgh.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220