C#2.0介绍了一个新特性--匿名方法,允许开发者在线(inline)声明自己的函数代码而无须使用委托函数(delegate function)。C#3.0中提供了一个新特性--Lambda表达式,它提供了完成相同目标的更加简洁的格式。让我们在讨论Lambda表达式以前仔细研究一下匿名方法。
匿名方法
假设你需要创建一个按钮,当点击它的时候更新ListBox里的内容。在C#1.0和1.1里,你要这样做:
public MyForm() { listBox = new ListBox(...); textBox = new TextBox(...); addButton = new Button(...); addButton.Click += new EventHandler(AddClick); } void AddClick(object sender, EventArgs e) { listBox.Items.Add(textBox.Text); } |
在C#2.0里,你需要这样做:
public MyForm() { listBox = new ListBox(...); textBox = new TextBox(...); addButton = new Button(...); addButton.Click += delegate { listBox.Items.Add(textBox.Text); }; |
就像你看到的一样,你不必要特别的声明一个新方法来将它连接到一个事件上。你可以在C#2.0里使用匿名方法来完成同样的工作。C#3.0里介绍了一种更加简单的格式,Lambda表达式,你可以直接使用"=>"来书写你的表达式列表,后面跟上一个表达式或者语句块。
Lambda表达式中的参数
Lambda表达式中的参数可以是显式或者隐式类型的。在一个显式类型参数列表里,每个表达式的类型是显式指定的。在一个隐式类型参数列表里,类型是通过上下文推断出来的:
(int x) => x + 1 // 显式类型参数 (y,z) => return y * z; // 隐式类型参数 |
Lambda演算实例
下面的例子给出了两种不同的方法来打印出一个list中长度为偶数的字符串。第一种方法AnonMethod使用了匿名方法,第二种LambdaExample则是通过Lambda演算实现:
// Program.cs using System; using System.Collections.Generic; using System.Text; using System.Query; using System.Xml.XLinq; using System.Data.DLinq; namespace LambdaExample { public delegate bool KeyValueFilter<K, V>(K key, V value); static class Program { static void Main(string[] args) { List<string> list = new List<string>(); list.Add("AA"); list.Add("ABC"); list.Add("DEFG"); list.Add("XYZ"); Console.WriteLine("Through Anonymous method"); AnonMethod(list); Console.WriteLine("Through Lambda expression"); LambdaExample(list); Dictionary<string, int> varClothes= new Dictionary<string,int>(); varClothes.Add("Jeans", 20); varClothes.Add("Shirts", 15); varClothes.Add("Pajamas", 9); varClothes.Add("Shoes", 9); var ClothesListShortage = varClothes.FilterBy((string name, int count) => name == "Shoes" && count < 10); // example of multiple parameters if(ClothesListShortage.Count > 0) Console.WriteLine("We are short of shoes"); Console.ReadLine(); } static void AnonMethod(List<string> list) { List<string> evenNumbers = list.FindAll(delegate(string i) { return (i.Length % 2) == 0; }); foreach (string evenNumber in evenNumbers) { Console.WriteLine(evenNumber); } } static void LambdaExample(List<string> list) { var evenNumbers = list.FindAll(i =>(i.Length % 2) == 0); // example of single parameter foreach(string i in evenNumbers) { Console.WriteLine(i); } } } public static class Extensions { public static Dictionary<K, V> FilterBy<K, V> (this Dictionary<K, V> items, KeyValueFilter<K, V> filter) { var result = new Dictionary<K, V>(); foreach(KeyValuePair<K, V> element in items) { if (filter(element.Key, element.Value)) result.Add(element.Key, element.Value); } return result; } } } |
多参数的Lambda表达式
Lambda表达式可以带上多个参数,比如你可以声明一个Dictionary类型:
Clothing Type | Count |
Shirts | 15 |
Jeans | 12 |
Shoes | 9 |
Pajamas | 9 |
如果你有一个匿名方法(FilterBy)来通过键和值来过滤字典,按么你可以传递多个参数给lambda表达式来调用这个匿名方法。附带的代码完成了这个FilterBy的功能:
var ClothesListShortage = clothesList.FilterBy((string name, int count) |
下面看一个列子,对各种方式做一下比较:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestClass
{
class Test
{
static void Main(string[] args)
{
Console.WriteLine("这是传统的代码,不含Lambda表达式");
FindListDelegate();
Console.WriteLine("这是匿名方法代码,不含Lambda表达式");
FindListBook();
Console.WriteLine("这是Lambda代码");
FindBookByLambda();
Console.WriteLine("这是Lambda多条代码块");
FindBookByL();
Console.WriteLine("这是Lambda多个参数");
Book book = new Book();
//当Lambda 表达式中的参数为空时,可以用“()”来代替
book.SetOuttockHandler((name, price)=> Console.WriteLine("要出售的书:{0}, 价格:{1}", name, price));
book.OutBook("Jquery实战", 49.00);
Console.ReadLine();
}
/// <summary>
/// 这里使用了传统的代码模式,既方法要明确声明
/// IsbookCatefory这个方法明确声明
/// List泛型中 重载了FindAll这个方法,它可以接受一个委托作为参数,进行判断
/// </summary>
public static void FindListDelegate()
{
List<string> list = new List<string>();
list.AddRange(new string[] { "武侠小说", "开发设计", "学习", "灵异小说" });
Predicate<string> findPredicate = new Predicate<string>(IsBookCategory);
List<string> bookCategory = list.FindAll(findPredicate);//List泛型中 重载了FindAll这个方法
foreach (var str in bookCategory)
{
Console.WriteLine("{0}\t", str);
}
}
public static bool IsBookCategory(string str)
{
return str.EndsWith("说") ? true : false;//endWitn()这是判断以什么字符结尾的
}
/// <summary>
/// 这里则是用了匿名方法
/// 匿名方法直接委托创建了一个代码块,无需另外创建单独方法
/// </summary>
static void FindListBook()
{
List<string> list = new List<string>();
list.AddRange(new string[] { "武侠小说","仙侠小说","玄幻小说","编程书","文学书","图书"});
List<string> bookCagtegory = list.FindAll(delegate(string str)
{
return str.EndsWith("说") ? true : false;
}
);
foreach (var str in bookCagtegory)
{
Console.WriteLine("{0}\t", str);
}
}
/// <summary>
/// 这里则使用了Lambda表达式
/// </summary>
static void FindBookByLambda()
{
List<string> list=new List <string>();
list.AddRange(new string[] { "武侠小说", "仙侠小说", "玄幻小说", "编程书", "文学书", "图书" });
List<string> bookCategory = list.FindAll(str => str.EndsWith("说"));//Lambda表达式的形式,用Lambda 创建了个委托方法
foreach (var str in bookCategory)
{
Console.WriteLine("{0}\t", str);
}
}
/// <summary>
/// Lambda表达式中多个执行语块的使用
/// </summary>
static void FindBookByL()
{
List<string> list = new List<string>();
list.AddRange(new string[] { "武侠小说", "仙侠小说", "玄幻小说", "编程书", "文学书", "图书" });
List<string> bookCateory = list.FindAll((str) => //处理参数要用“()”
{
Console.WriteLine("要比较的字符为\t:{0}", str);
return str.EndsWith("书");
});
foreach (var str in bookCateory)
{
Console.WriteLine("{0}\t", str);
}
}
/// <summary>
/// 多个参数的Lambda 表达式
/// </summary>
//定义一个Book类,在类中使用委托
class Book
{
//定义了一个出库的委托
public delegate void Outstock(string name, double price);
private Outstock outstock;
public void SetOuttockHandler(Outstock target)
{
outstock = target;
}
//定义一个出库方法
public void OutBook(string name, double price)
{
if (outstock != null)
{
outstock.Invoke(name, price);
}
}
}
}
}