马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

IEnumerable接口的使用方法

  [复制链接]
查看4716 | 回复5 | 2014-8-26 17:39:48 | 显示全部楼层 |阅读模式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace demo4
{
    public class Garage : IEnumerable
    {
        Car[] carArrary=new Car[4];
        public Garage()
        {
            carArrary[0] = new Car("a",100);
            carArrary[1] = new Car("b", 200);
            carArrary[2] = new Car("c", 300);
            carArrary[3] = new Car("d", 400);
        }
        public IEnumerator GetEnumerator()
        {
            return this.carArrary.GetEnumerator();//返回本类的一个IEnumerator的遍历对象
        }  

    }
    public class Car
    {
        public string name;
        public int Speed;
        public Car(string name,int Speed)
        {
            this.name = name;
            this.Speed = Speed;
        }

    }
}


using System;
using System.Collections;
using System.Linq;
using System.Text;

namespace demo4
{
    class Program
    {
        static void Main(string[] args)
        {
            Garage a = new Garage();
            foreach(Car b in a)
            {
                Console.WriteLine("{0} is going {1} MPH", b.name, b.Speed);
            }
            Console.WriteLine("GetEnumerator被定义为公开的,对象用户可以与IEnumerator类型交互,下面的结果与上面是一致的");
            IEnumerator i = a.GetEnumerator();
            while (i.MoveNext())
            {
                Car myCar = (Car)i.Current;
                Console.WriteLine("{0} is going {1} MPH", myCar.name, myCar.Speed);
            }
            Console.ReadLine();  
            Console.ReadKey();
        }
    }
}




ibcadmin | 2014-8-26 20:54:04 | 显示全部楼层
我看了你这三个教程  其实都是EF框架有关

点评

是的  详情 回复 发表于 2014-8-27 05:53
C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
惜鑫 | 2014-8-26 20:55:06 | 显示全部楼层
终于看到教程了!
师兄阿杜 | 2014-8-26 21:00:39 | 显示全部楼层
表示没看懂

点评

细心看  详情 回复 发表于 2014-8-27 05:53
Testing_C# | 2014-8-27 05:53:17 | 显示全部楼层
ibcadmin 发表于 2014-8-26 20:54
我看了你这三个教程  其实都是EF框架有关

是的
Testing_C# | 2014-8-27 05:53:49 | 显示全部楼层

细心看   
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则