Lambda表达式

C#2.0介绍了一个新特性--匿名方法,允许开发者在线(inline)声明自己的函数代码而无须使用委托函数(delegate function)。C#3.0中提供了一个新特性--Lambda表达式,它提供了完成相同目标的更加简洁的格式。让我们在讨论Lambda表达式以前 仔细研究一下匿名方法。

  匿名方法

  假设你需要创建一个按钮,当点击它的时候更新ListBox里的内容。在C#1.0和1.1里,你要这样做:
  1. public MyForm()
  2. {
  3.  listBox = new ListBox(...);
  4.  textBox = new TextBox(...);
  5.  addButton = new Button(...);
  6.  addButton.Click += new EventHandler(AddClick);
  7. }
  8. void AddClick(object sender, EventArgs e)
  9. {
  10.  listBox.Items.Add(textBox.Text);
在C#2.0里,你需要这样做:
  1. public MyForm()
  2. {
  3.  listBox = new ListBox(...);
  4.  textBox = new TextBox(...);
  5.  addButton = new Button(...);
  6.  addButton.Click += delegate
  7.  {
  8.   listBox.Items.Add(textBox.Text);
  9. };
就像你看到的一样,你不必要特别的声明一个新方法来将它连接到一个事件上。你可以在C#2.0里使用匿名方法来完成同样的工作。C#3.0里介绍了一种更 加简单的格式,Lambda表达式,你可以直接使用"=>"来书写你的表达式列表,后面跟上一个表达式或者语句块。

  Lambda表达式中的参数

  Lambda表达式中的参数可以是显式或者隐式类型的。在一个显式类型参数列表里,每个表达式的类型是显式指定的。在一个隐式类型参数列表里,类型是通过上下文推断出来的:
  1. (int x) => x + 1 // 显式类型参数
  2. (y,z) => return y * z; // 隐式类型参数 
Lambda演算实例

  下面的例子给出了两种不同的方法来打印出一个list中长度为偶数的字符串。第一种方法AnonMethod使用了匿名方法,第二种LambdaExample则是通过Lambda演算实现:
  1. // Program.cs
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Query;
  6. using System.Xml.XLinq;
  7. using System.Data.DLinq;
  8. namespace LambdaExample
  9. {
  10.  public delegate bool KeyValueFilter<K, V>(K key, V value);
  11.  static class Program
  12.  {
  13.   static void Main(string[] args)
  14.   {
  15.    List<string> list = new List<string>();
  16.    list.Add("AA");
  17.    list.Add("ABC");
  18.    list.Add("DEFG");
  19.    list.Add("XYZ");
  20.    Console.WriteLine("Through Anonymous method");
  21.    AnonMethod(list);
  22.    Console.WriteLine("Through Lambda expression");
  23.    LambdaExample(list);
  24.    Dictionary<stringint> varClothes= new Dictionary<string,int>();
  25.    varClothes.Add("Jeans", 20);
  26.    varClothes.Add("Shirts", 15);
  27.    varClothes.Add("Pajamas", 9);
  28.    varClothes.Add("Shoes", 9);
  29.    var ClothesListShortage = varClothes.FilterBy((string name,
  30.    int count) => name == "Shoes" && count < 10);
  31.    // example of multiple parameters
  32.    if(ClothesListShortage.Count > 0)
  33.     Console.WriteLine("We are short of shoes");
  34.    Console.ReadLine();
  35.  }
  36.  static void AnonMethod(List<string> list)
  37.  {
  38.   List<string> evenNumbers = list.FindAll(delegate(string i)
  39.   { return (i.Length % 2) == 0; });
  40.   foreach (string evenNumber in evenNumbers)
  41.   {
  42.    Console.WriteLine(evenNumber);
  43.   }
  44.  }
  45.  static void LambdaExample(List<string> list)
  46.  {
  47.   var evenNumbers = list.FindAll(i =>(i.Length % 2) == 0); // example of single parameter
  48.   foreach(string i in evenNumbers)
  49.   {
  50.    Console.WriteLine(i);
  51.   }
  52.  }
  53. }
  54. public static class Extensions
  55. {
  56.  public static Dictionary<K, V> FilterBy<K, V>
  57. (this Dictionary<K, V> items, KeyValueFilter<K, V> filter)
  58.  {
  59.   var result = new Dictionary<K, V>();
  60.   foreach(KeyValuePair<K, V> element in items)
  61.   {
  62.    if (filter(element.Key, element.Value))
  63.     result.Add(element.Key, element.Value);
  64.   }
  65.   return result;
  66.  }
  67.  
  68. }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值