IENumerable_Test
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace IENumerable_Test
{ public class Person
{
public string firstName;
public string lastName; public Person(string fName, string lName)
{
this.firstName = fName;
this.lastName = lName;
}
} public class People : IEnumerable
{
private Person[] _people; public People(Person[] pArray)
{
this._people = 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();
}
//IEnumerator IEnumerable.GetEnumerator()
// {
// throw new NotImplementedException();
// } public PeopleEnum GetEnumerator()
{
return new PeopleEnum(_people);
}
} 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)
{
this._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 Program
{
static void Main(string[] args)
{
Person[] peopleArray = { 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);
Console.ReadKey();
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace test
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("*********Fun with IEnumberable/IEnumerator************\n");
Garage carLot = new Garage(); //交出集合中的每一Car对象吗
foreach (Car c in carLot)
{
Console.WriteLine("{0} is going {1} MPH", c.Name, c.Speed);
} Console.WriteLine("GetEnumerator被定义为公开的,对象用户可以与IEnumerator类型交互,下面的结果与上面是一致的"); IEnumerator i = carLot.GetEnumerator();
while(i.MoveNext())
{
//i.current返回值类型是object的
Car myCar = (Car)i.Current;
Console.WriteLine("{0} is going {1} MPH", myCar.Name, myCar.Speed);
} Console.ReadLine();
}
} public class Garage : IEnumerable
{
Car[] carArray = new Car[4]; //在Garage中定义一个Car类型的数组carArray,其实carArray在这里的本质是一个数组字段 //启动时填充一些Car对象
public Garage()
{
//为数组字段赋值
carArray[0] = new Car("Rusty", 30);
carArray[1] = new Car("Clunker", 50);
carArray[2] = new Car("Zippy", 30);
carArray[3] = new Car("Fred", 45);
} public IEnumerator GetEnumerator()
{
//递归调用
return this.carArray.GetEnumerator();
} } public class Car
{
public string Name { get; set; }
public int Speed { get; set; } public Car(string name, int speed)
{
this.Name = name;
this.Speed = speed;
}
}
}
IENumerable_Test的更多相关文章
随机推荐
- DataTable转对象Model
我们经常需要从数据库表中取数,取数是以取DataTable的方式,但是我么希望以对象为单位进行这种操作.即存在把取到的DataTable(数据表)转换为ModelList(对象集合)的需求. 原理稍微 ...
- 解析Mybatis入门第一天
MyBatis是一个基于Java的持久层框架,内部对JDBC做了封装,使开发者只需要关注SQL语句,而不用关注JDBC的代码,使开发变得更加的简单. MyBatis通过XML或者注解的方式将要执行的各 ...
- [HL] 7.5 集训总结
对于某唤做赛区难度的题,我只能是内流满面..拿到题,A神题不可做,B,神题不可做,C,神题不可做...最后yy了一个A的算法...只得了20 TAT.C题骗分似乎有50 ..B题本来想骗分..然后/ ...
- 58 matlab 编程
0 引言 matlab中有些东西记录一下 1 matlab coder matlab命令行窗口输入: coder 回车即可打开matlab coder 窗口.接着,matlab将引导你把matlab格 ...
- 暑假集训test-8-31(am)
1.字符串匹配 看到题目以为真是字符串题结果是数学题..70分做法很傻逼然而我更傻逼只有30... 正解是发现两个位置会匹配当且仅当mod gcd(lena,lenb)同余,在一个lcm(lena,l ...
- NX二次开发-UFUN特征找xxx UF_MODL_ask_feat_xxx等函数(待补充)
NX9+VS2012 #include <uf.h> #include <uf_modl.h> #include <uf_obj.h> #include <u ...
- (转)简述负载均衡&CDN技术
转:http://www.cnblogs.com/mokafamily/p/4402366.html#commentform 曾经见到知乎上有人问“为什么像facebook这类的网站需要上千个工程师维 ...
- 其它课程中的python---2、NumPy模块
其它课程中的python---2.NumPy模块 一.总结 一句话总结: numpy在数组计算方面又快又方便 1.NumPy中的ndarray是一个多维数组对象,该对象由哪两部分组成? -实际的数据 ...
- 剑指offer——11矩阵覆盖
题目描述 我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形.请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法? 题解: 使用递归或者动态规划,明显,递归没有动态规划优 ...
- java 冒泡排序法、选择排序
1.冒泡排序 /* * 冒泡排序 * 外层控制循环多少趟,内层控制每一趟的循环次数 */ public class Test08 { public static void main(String[] ...