C#中IEnumerable接口介绍并实现自定义集合
简介
IEnumerable接口是非常的简单,只包含一个抽象的方法GetEnumerator(),它返回一个可用于循环访问集合的IEnumerator对象。对于所有数组的遍历,都来自IEnumerable接口。
IEnumerator对象有什么呢?它是一个真正的集合访问器,没有它,就不能使用foreach语句遍历集合或数组,因为只有IEnumerator对象才能访问集合中的项,假如连集合中的项都访问不了,那么进行集合的循环遍历是不可能的事情了。
一、foreach在IEnumerable中案例
public static void Test3()
{
MyInt temp = new MyInt();
foreach (int item in temp)
Console.WriteLine(item);
}
//foreach的必须要实现IEnumerable和IEnumerator的接口
public class MyInt : IEnumerable
{
int[] temp = { 1, 32, 43, 343 };
public IEnumerator GetEnumerator()
{
return temp.GetEnumerator();
}
}相当于下面代码:
public static void Test1()
{
int[] myArray = { 1, 32, 43, 343 };
//获取要遍历的枚举数
IEnumerator myie = myArray.GetEnumerator();
//重置当前项,相当于把指针移到初始位置:position = -1; 一开始认识数组的索引从“0”开始
myie.Reset();
//向前移动一个索引,返回Bool类型,判断是否超出下标
while (myie.MoveNext())
{
int i = (int)myie.Current;//从Object转成对应类型
Console.WriteLine("Value: {0}", i);
}
}包含一个属性两个方法
MoveNext:把当前的项移动到下一项(类似于索引值),返回一个bool值,这个bool值用来检查当前项是否超出了枚举数的范围!
Current:获取当前项的值,返回一个object的类型!
Reset:顾名思义也就是把一些值恢复为默认值,比如把当前项恢复到默认状态值!
二、Lamda在IEnumerable中案例
//lamda表达式在数组中查询
public static void Test2()
{
List fruits =
new List { "apple", "passionfruit", "banana", "mango",
"orange", "blueberry", "grape", "strawberry" };
//List query = fruits.Where(fruit => fruit.Length < 6).ToList();
IEnumerable query = fruits.Where(fruit => fruit.Length < 6);
foreach (string fruit in query)
Console.WriteLine(fruit);
} 只筛选出List中的元素长度小于6的值,然后打印出。
实现自定义集合的 IEnumerable和IEnumerator 接口
namespace ConsoleApplication1
{
//定义Person类
public class Person
{
//初始化
public Person(string fName, string lName)
{
this.firstName = fName;
this.lastName = lName;
}
//类成员
public string firstName;
public string lastName;
}
//实现接口
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];
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return (IEnumerator)GetEnumerator();
}
//获取枚举数
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)
{
_people = list;
}
//向下推移索引,返回Bool类型值
public bool MoveNext()
{
position++;
return (position < _people.Length);
}
//重置默认索引位置,默认下标为0
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
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);
}
}
}到此这篇关于C#中IEnumerable接口并实现自定义集合的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
上一篇:C语言深入讲解内存操作问题
下一篇:Java实现抽奖算法的示例代码
X 关闭
X 关闭
- 15G资费不大降!三大运营商谁提供的5G网速最快?中国信通院给出答案
- 2联想拯救者Y70发布最新预告:售价2970元起 迄今最便宜的骁龙8+旗舰
- 3亚马逊开始大规模推广掌纹支付技术 顾客可使用“挥手付”结账
- 4现代和起亚上半年出口20万辆新能源汽车同比增长30.6%
- 5如何让居民5分钟使用到各种设施?沙特“线性城市”来了
- 6AMD实现连续8个季度的增长 季度营收首次突破60亿美元利润更是翻倍
- 7转转集团发布2022年二季度手机行情报告:二手市场“飘香”
- 8充电宝100Wh等于多少毫安?铁路旅客禁止、限制携带和托运物品目录
- 9好消息!京东与腾讯续签三年战略合作协议 加强技术创新与供应链服务
- 10名创优品拟通过香港IPO全球发售4100万股 全球发售所得款项有什么用处?

