新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

IEnumerable接口-创新互联

=================================================简单的实现IEnumerable接口

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:空间域名、虚拟空间、营销软件、网站建设、杭锦网站维护、网站推广。

------------------------------------Person.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace ConsoleApplication6
{
    public class Person
    {
        public string Name { get; set; }
    }
}

------------------------------------PerAll.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace ConsoleApplication6
{
    public class PerAll:IEnumerable
    {
        Person[] p = new Person[3];//实例化长度为三个的数组
        public PerAll()//构造函数初始化数组
        {
            p[0] = new Person { Name = "张三" };
            p[1] = new Person { Name = "李四" };
            p[2] = new Person { Name = "王五" };
        }
        public IEnumerator GetEnumerator()//迭代器
        {
            return p.GetEnumerator();//直接调用数组自带的的GetEnumerator。(简单委托请求到System.Array)
        }
    }
}

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

            PerAll p = new PerAll();//实例化对象
            //-------------------第一种方式遍历
            foreach (Person  item in p)
            {
                Console.WriteLine(item.Name);
            }
            //-------------------第二中方式遍历
            IEnumerator ie = p.GetEnumerator();
            while (ie.MoveNext())
            {
                Console.WriteLine((ie.Current as Person).Name);
            } 
            
            
            
            //普通数组遍历
            string[] str = new string[3] { "张辽", "张合", "张飞" };
            IEnumerator strie = str.GetEnumerator();
            while (strie.MoveNext())
            {
                Console.WriteLine(strie.Current as string);
            }

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


网页名称:IEnumerable接口-创新互联
新闻来源:http://www.wjwzjz.com/article/ddihjg.html
在线咨询
服务热线
服务热线:028-86922220
TOP