c# IEnumerable和IEnumerator & Lambda表达式
2015-10-20 17:04
851 查看
IEnumerable和IEnumerator 详解
初学C#的时候,老是被IEnumerable、IEnumerator、ICollection等这样的接口弄的糊里糊涂,我觉得有必要切底的弄清楚IEnumerable和IEnumerator的本质。下面我们先看IEnumerable和IEnumerator两个接口的语法定义。其实IEnumerable接口是非常的简单,只包含一个抽象的方法GetEnumerator(),它返回一个可用于循环访问集合的IEnumerator对象。IEnumerator对象有什么呢?它是一个真正的集合访问器,没有它,就不能使用foreach语句遍历集合或数组,因为只有IEnumerator对象才能访问集合中的项,假如连集合中的项都访问不了,那么进行集合的循环遍历是不可能的事情了。那么让我们看看IEnumerator接口有定义了什么东西。看下图我们知道IEnumerator接口定义了一个Current属性,MoveNext和Reset两个方法,这是多么的简约。既然IEnumerator对象时一个访问器,那至少应该有一个Current属性,来获取当前集合中的项吧。
MoveNext方法只是将游标的内部位置向前移动(就是移到一下个元素而已),要想进行循环遍历,不向前移动一下怎么行呢?
![](http://hi.csdn.net/attachment/201201/1/0_1325428292Ebm9.gif)
![](http://hi.csdn.net/attachment/201201/1/0_13254284991w8d.gif)
详细讲解:
说到IEnumerable总是会和IEnumerator、foreach联系在一起。
C# 支持关键字foreach,允许我们遍历任何数组类型的内容:
//遍历数组的项
int[] myArrayOfInts = {10,20,30,40};
foreach(int i in my myArrayOfInts)
{
Console.WirteLine(i);
}
虽然看上去只有数组才可以使用这个结构,其实任何支持GetEnumerator()方法的类型都可以通过foreach结构进行运算。
[csharp]
view plaincopy
public class Garage
{
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);
}
}
理想情况下,与数据值数组一样,使用foreach构造迭代Garage对象中的每一个子项比较方便:
[csharp]
view plaincopy
//这看起来好像是可行的
lass 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.CarName, c.CurrentSpeed);
}
Console.ReadLine();
}
}
让人沮丧的是,编译器通知我们Garage类没有实现名为GetEnumerator()的方法(显然用foreach遍历Garage对象是不可能的事情,因为Garage类没有实现GetEnumerator()方法,Garage对象就不可能返回一个IEnumerator对象,没有IEnumerator对象,就不可能调用方法MoveNext(),调用不了MoveNext,就不可能循环的了)。这个方法是有隐藏在System.collections命名空间中的IEnumerable接口定义的。(特别注意,其实我们循环遍历的都是对象而不是类,只是这个对象是一个集合对象)
支持这种行为的类或结构实际上是宣告它们向调用者公开所包含的子项:
//这个接口告知调方对象的子项可以枚举
public interface IEnumerable
{
IEnumerator GetEnumerator();
}
可以看到,GetEnumerator方法返回对另一个接口System.Collections.IEnumerator的引用。这个接口提供了基础设施,调用方可以用来移动IEnumerable兼容容器包含的内部对象。
//这个接口允许调用方获取一个容器的子项
public interface IEnumerator
{
bool MoveNext(); //将游标的内部位置向前移动
object Current{get;} //获取当前的项(只读属性)
void Reset(); //将游标重置到第一个成员前面
}
所以,要想Garage类也可以使用foreach遍历其中的项,那我们就要修改Garage类型使之支持这些接口,可以手工实现每一个方法,不过这得花费不少功夫。虽然自己开发GetEnumerator()、MoveNext()、Current和Reset()也没有问题,但有一个更简单的办法。因为System.Array类型和其他许多类型(如List)已经实现了IEnumerable和IEnumerator接口,你可以简单委托请求到System.Array,如下所示:
[csharp]
view plaincopy
namespace MyCarIEnumerator
{
public class Garage:IEnumerable
{
Car[] carArray = new Car[4];
//启动时填充一些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();
}
}
}
//修改Garage类型之后,就可以在C#foreach结构中安全使用该类型了。
[csharp]
view plaincopy
//除此之外,GetEnumerator()被定义为公开的,对象用户可以与IEnumerator类型交互:
namespace MyCarIEnumerator
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("*********Fun with IEnumberable/IEnumerator************\n");
Garage carLot = new Garage();
//交出集合中的每一Car对象吗
foreach (Car c in carLot) //之所以遍历carLot,是因为carLot.GetEnumerator()返回的项时Car类型,这个十分重要
{
Console.WriteLine("{0} is going {1} MPH", c.CarName, c.CurrentSpeed);
}
Console.WriteLine("GetEnumerator被定义为公开的,对象用户可以与IEnumerator类型交互,下面的结果与上面是一致的");
//手动与IEnumerator协作
IEnumerator i = carLot.GetEnumerator();
while (i.MoveNext())
{
Car myCar = (Car)i.Current;
Console.WriteLine("{0} is going {1} MPH", myCar.CarName, myCar.CurrentSpeed);
}
Console.ReadLine();
}
}
}
下面我们来看看手工实现IEnumberable接口和IEnumerator接口中的方法:
[csharp]
view plaincopy
namespace ForeachTestCase
{
//继承IEnumerable接口,其实也可以不继承这个接口,只要类里面含有返回IEnumberator引用的GetEnumerator()方法即可
class ForeachTest:IEnumerable {
private string[] elements; //装载字符串的数组
private int ctr = 0; //数组的下标计数器
/// <summary>
/// 初始化的字符串
/// </summary>
/// <param name="initialStrings"></param>
ForeachTest(params string[] initialStrings)
{
//为字符串分配内存空间
elements = new String[8];
//复制传递给构造方法的字符串
foreach (string s in initialStrings)
{
elements[ctr++] = s;
}
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="source">初始化的字符串</param>
/// <param name="delimiters">分隔符,可以是一个或多个字符分隔</param>
ForeachTest(string initialStrings, char[] delimiters)
{
elements = initialStrings.Split(delimiters);
}
//实现接口中得方法
public IEnumerator GetEnumerator()
{
return new ForeachTestEnumerator(this);
}
private class ForeachTestEnumerator : IEnumerator
{
private int position = -1;
private ForeachTest t;
public ForeachTestEnumerator(ForeachTest t)
{
this.t = t;
}
#region 实现接口
public object Current
{
get
{
return t.elements[position];
}
}
public bool MoveNext()
{
if (position < t.elements.Length - 1)
{
position++;
return true;
}
else
{
return false;
}
}
public void Reset()
{
position = -1;
}
#endregion
}
static void Main(string[] args)
{
// ForeachTest f = new ForeachTest("This is a sample sentence.", new char[] { ' ', '-' });
ForeachTest f = new ForeachTest("This", "is", "a", "sample", "sentence.");
foreach (string item in f)
{
System.Console.WriteLine(item);
}
Console.ReadKey();
}
}
}
IEnumerable<T>接口
实现了IEnmerable<T>接口的集合,是强类型的。它为子对象的迭代提供类型更加安全的方式。
[csharp]
view plaincopy
public class ListBoxTest:IEnumerable<String>
{
private string[] strings;
private int ctr = 0;
#region IEnumerable<string> 成员
//可枚举的类可以返回枚举
public IEnumerator<string> GetEnumerator()
{
foreach (string s in strings)
{
yield return s;
}
}
#endregion
#region IEnumerable 成员
//显式实现接口
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
//用字符串初始化列表框
public ListBoxTest(params string[] initialStrings)
{
//为字符串分配内存空间
strings = new String[8];
//复制传递给构造方法的字符串
foreach (string s in initialStrings)
{
strings[ctr++] = s;
}
}
//在列表框最后添加一个字符串
public void Add(string theString)
{
strings[ctr] = theString;
ctr++;
}
//允许数组式的访问
public string this[int index]
{
get {
if (index < 0 || index >= strings.Length)
{
//处理不良索引
}
return strings[index];
}
set {
strings[index] = value;
}
}
//发布拥有的字符串数
public int GetNumEntries()
{
return ctr;
}
}
[csharp]
view plaincopy
class Program
{
static void Main(string[] args)
{
//创建一个新的列表框并初始化
ListBoxTest lbt = new ListBoxTest("Hello", "World");
//添加新的字符串
lbt.Add("Who");
lbt.Add("Is");
lbt.Add("Douglas");
lbt.Add("Adams");
//测试访问
string subst = "Universe";
lbt[1] = subst;
//访问所有的字符串
foreach (string s in lbt)
{
Console.WriteLine("Value:{0}", s);
}
Console.ReadKey();
}
}
综上所述,一个类型是否支持foreach遍历,必须满足下面条件:
方案1:让这个类实现IEnumerable接口
方案2:这个类有一个public的GetEnumerator的实例方法,并且返回类型中有public 的bool MoveNext()实例方法和public的Current实例属性。
C# Lambda表达式
Lambda表达式"Lambda表达式"是一个匿名函数,是一种高效的类似于函数式编程的表达式,Lambda简化了开发中需要编写的代码量。它可以包含表达式和语句,并且可用于创建委托或表达式目录树类型,支持带有可绑定到委托或表达式树的输入参数的内联表达式。所有Lambda表达式都使用Lambda运算符=>,该运算符读作"goes to"。Lambda运算符的左边是输入参数(如果有),右边是表达式或语句块。Lambda表达式x => x * x读作"x goes to x times x"。可以将此表达式分配给委托类型,如下所示:
delegate int del(int i);
del myDelegate = x => x * x;
int j = myDelegate(5); //j = 25
Lambda表达式Lambda表达式是由.NET 2.0演化而来的,也是LINQ的基础,熟练地掌握Lambda表达式能够快速地上手LINQ应用开发。
Lambda表达式在一定程度上就是匿名方法的另一种表现形式。为了方便对Lambda表达式的解释,首先需要创建一个People类,示例代码如下。
public class People
{
public int age { get; set; } //设置属性
public string name { get; set; } //设置属性
public People(int age,string name) //设置属性(构造函数构造)
{
this.age = age; //初始化属性值age
this.name = name; //初始化属性值name
}
}
上述代码定义了一个People类,并包含一个默认的构造函数能够为People对象进行年龄和名字的初始化。在应用程序设计中,很多情况下需要创建对象的集合,创建对象的集合有利于对对象进行搜索操作和排序等操作,以便在集合中筛选相应的对象。使用List进行泛型编程,可以创建一个对象的集合,示例代码如下。
List<People> people = new List<People>(); //创建泛型对象
People p1 = new People(21,"guojing"); //创建一个对象
People p2 = new People(21, "wujunmin"); //创建一个对象
People p3 = new People(20, "muqing"); //创建一个对象
People p4 = new People(23, "lupan"); //创建一个对象
people.Add(p1); //添加一个对象
people.Add(p2); //添加一个对象
people.Add(p3); //添加一个对象
people.Add(p4); //添加一个对象
上述代码创建了4个对象,这4个对象分别初始化了年龄和名字,并添加到List列表中。当应用程序需要对列表中的对象进行筛选时,例如需要筛选年龄大于20岁的人,就需要从列表中筛选,示例代码如下。
//匿名方法
IEnumerable<People> results = people.Where
(delegate(People p) { return p.age > 20; });
上述代码通过使用IEnumerable接口创建了一个result集合,并且该集合中填充的是年龄大于20的People对象。细心的读者能够发现在这里使用了一个匿名方法进行筛选,因为该方法没有名称,通过使用People类对象的age字段进行筛选。
虽然上述代码中执行了筛选操作,但是,使用匿名方法往往不太容易理解和阅读,而Lambda表达式则更加容易理解和阅读,示例代码如下。
IEnumerable<People> results = people.Where(People => People.age > 20);
上述代码同样返回了一个People对象的集合给变量results,但是,其编写的方法更加容易阅读,从这里可以看出Lambda表达式在编写的格式上和匿名方法非常相似。其实,当编译器开始编译并运行时,Lambda表达式最终也表现为匿名方法。
使用匿名方法并不是创建了没有名称的方法,实际上编译器会创建一个方法,这个方法对于开发人员来说是不可见的,该方法会将People类的对象中符合p.age>20的对象返回并填充到集合中。相同地,使用Lambda表达式,当编译器编译时,Lambda表达式同样会被编译成一个匿名方法进行相应的操作,但是与匿名方法相比,Lambda表达式更容易阅读,Lambda表达式的格式如下。
(参数列表)=>表达式或语句块
上述代码中,参数列表就是People类,表达式或语句块就是People.age>20,使用Lambda表达式能够让人很容易地理解该语句究竟是如何执行的,虽然匿名方法提供了同样的功能,却不容易被理解。相比之下,People => People.age > 20却能够很好地理解为"返回一个年纪大于20的人"。其实,Lambda表达式并没有什么高深的技术,Lambda表达式可以看作是匿名方法的另一种表现形式。Lambda表达式经过反编译后,与匿名方法并没有什么区别。
比较Lambda表达式和匿名方法,在匿名方法中,"("、")"内是方法的参数的集合,这就对应了Lambda表达式中的"(参数列表)",而匿名方法中"{"、"}"内是方法的语句块,这对应了Lambda表达式中"=>"符号右边的表达式或语句块项。Lambda表达式也包含一些基本的格式,这些基本格式如下。
Lambda表达式可以有多个参数、一个参数,或者没有参数。其参数类型可以隐式或者显式。示例代码如下:
(x, y) => x * y //多参数,隐式类型=> 表达式
x => x * 5 //单参数, 隐式类型=>表达式
x => { return x * 5; } //单参数,隐式类型=>语句块
(int x) => x * 5 //单参数,显式类型=>表达式
(int x) => { return x * 5; } //单参数,显式类型=>语句块
() => Console.WriteLine() //无参数
上述格式都是Lambda表达式的合法格式,在编写Lambda表达式时,可以忽略参数的类型,因为编译器能够根据上下文直接推断参数的类型,示例代码如下。
(x, y) => x + y //多参数,隐式类型=> 表达式
Lambda表达式的主体可以是表达式也可以是语句块,这样就节约了代码的编写。
【例2-5】传统方法,匿名方法和Lamdba表达式对比。
(1) 创建控制台应用程序LamdbaPrictice。
(2) 在程序中添加3个函数,这3个函数分别使用传统的委托调用、使用匿名方法和Lamdba表达式方法完成同一功能,对比有什么不同。代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LambdaDemo
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("传统的委托代码示例:");
FindListDelegate();
Console.Write("\n");
Console.WriteLine("使用匿名方法的示例:");
FindListAnonymousMethod();
Console.Write("\n");
Console.WriteLine("使用Lambda的示例:");
FindListLambdaExpression();
}
//传统的调用委托的示例
static void FindListDelegate()
{
//先创建一个泛型的List类
List<string> list = new List<string>();
list.AddRange(new string[] { "ASP.NET课程","J2EE课程", "PHP课程", "数据结构课程" });
Predicate<string> findPredicate = new Predicate<string>(IsBookCategory);
List<string> bookCategory = list.FindAll(findPredicate);
foreach (string str in bookCategory)
{
Console.WriteLine("{0}\t", str);
}
}
//谓词方法,这个方法将被传递给FindAll方法进行书书籍分类的判断
static bool IsBookCategory(string str)
{
return str.EndsWith("课程") ? true : false;
}
//使用匿名方法来进行搜索过程
static void FindListAnonymousMethod()
{
//先创建一个泛型的List类
List<string> list = new List<string>();
list.AddRange(new string[] { "ASP.NET课程", "J2EE课程", "PHP课程", "数据结构课程" });
//在这里,使用匿名方法直接为委托创建一个代码块,而不用去创建单独的方法
List<string> bookCategory = list.FindAll
(delegate(string str)
{
return str.EndsWith("课程") ? true : false;
}
);
foreach (string str in bookCategory)
{
Console.WriteLine("{0}\t", str);
}
}
//使用Lambda来实现搜索过程
static void FindListLambdaExpression()
{
//先创建一个泛型的List类
List<string> list = new List<string>();
list.AddRange(new string[] { "ASP.NET课程", "J2EE课程", "PHP课程", "数据结构课程" });
//在这里,使用了Lambda来创建一个委托方法
List<string> bookCategory = list.FindAll((string str) => str.EndsWith("课程"));
foreach (string str in bookCategory)
{
Console.WriteLine("{0}\t", str);
}
}
}
}
程序的运行结果如图2-7所示。
![]() |
图2-7 运行结果 |
相关文章推荐
- C# WinForm 单例模式(例:同一个窗体只创建一次实例)
- C#学习日记27----属性
- 如何有效的使用C#读取文件
- C#中的隐藏和重写
- c# XML和实体类之间相互转换(序列化和反序列化)
- C#内存优化
- C# 值类型和引用类型,堆和堆栈的区别
- 传智播客c#学习笔记1
- C#开发经典书籍
- [转]C#之反射
- C#中对EXCEL保存的SAVEAS方法说明
- C# 理解lock
- C#接口实现& is和as的含义与使用
- C#中正则表达式的使用
- C# 反射笔记
- c# 操作Word总结(四)——书签使用
- C#二维数组基本用法实例
- AX2009 C#客户端通过Web service批量审核工作流(二)
- c#提取word文档中的图片
- C#带ref和out的反射使用