c# 3.0新特性之使用Lambda表达式
2009年8月18日 云飞扬 发表评论 阅读评论
-
Working with Lambda Expressions 使用Lambda表达式
Lambda表达式有什么作用,它的使用方法是?
作用主要可以在匿名的方法的基础上,进一步简化代码,也可以隐藏delegate关键字
比如在2.0中代码如下:
var innerPoints = points.FindAll(delegate(Point p) { return (p.X > 0 && p.Y > 0); });
那么在C# 3.0 可以写为
var innerPoints = points.FindAll( p => p.X > 0 && p.Y > 0);
Lambda表达式的基本编写方法如以下代码所示:
(参数列表)=>{表达式};
表达式即事件处理方法体代码,参数列表是可选的,即完全可以省略参数列表的编写。
Lambda表达式中的参数可以是显式或者隐式类型的。在一个显式类型参数列表里,每个表达式的类型是显式指定的。在一个隐式类型参数列表里,类型是通过上下文推断出来的:
(int x) => x + 1 // 显式类型参数
(y,z) => return y * z; // 隐式类型参数
再举个例子
return customers.FindAll(c => c.City == city);
可以取代一下
return customers.FindAll(
delegate(Customer c)
{
return c.City == city;
});
完整的源码,查找城市名为London的数据并打印出来
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NewLanguageFeatures
{
public static class Extensions
{
public static List<T> Append<T>(this List<T> a, List<T> b)
{
var newList = new List<T>(a);
newList.AddRange(b);
return newList;
}
public static bool Compare(this Customer customer1, Customer customer2)
{
if (customer1.CustomerId == customer2.CustomerId &&
customer1.Name == customer2.Name &&
customer1.City == customer2.City)
{
return true;
}
return false;
}
}
public class Customer
{
public int CustomerId { get; private set; }
public string Name { get; set; }
public string City { get; set; }
public Customer(int Id)
{
CustomerId = Id;
}
public override string ToString()
{
return Name + "/t" + City + "/t" + CustomerId;
}
}
class Program
{
static void Main(string[] args)
{
var customers = CreateCustomers();
foreach (var c in FindCustomersByCity(customers, "London"))
Console.WriteLine(c);
}
public static List<Customer> FindCustomersByCity(
List<Customer> customers,string city)
{
//return customers.FindAll(
// delegate(Customer c)
// {
// return c.City == city;
// });
return customers.FindAll(c => c.City == city);
}
static List<Customer> CreateCustomers()
{
return new List<Customer>
{
new Customer(1) { Name = "Alex Roland", City = "Berlin" },
new Customer(2) { Name = "Oliver Cox", City = "Marseille" },
new Customer(3) { Name = "Maurice Taylor", City = "London" },
new Customer(4) { Name = "Phil Gibbins", City = "London" },
new Customer(5) { Name = "Tony Madigan", City = "Torino" },
new Customer(6) { Name = "Elizabeth A. Andersen", City = "Portland" },
new Customer(7) { Name = "Justin Thorp", City = "London" },
new Customer(8) { Name = "Bryn Paul Dunton", City = "Portland" }
};
}
}
}
一个更复杂的例子,查找打印姓名已T开头的人的信息
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NewLanguageFeatures
{
public delegate bool KeyValueFilter<K, V>(K key, V value);
public static class Extensions
{
public static List<K> FilterBy<K, V>(
this Dictionary<K, V> items,
KeyValueFilter<K, V> filter)
{
var result = new List<K>();
foreach (KeyValuePair<K, V> element in items)
{
if (filter(element.Key, element.Value))
result.Add(element.Key);
}
return result;
}
public static List<T> Append<T>(this List<T> a, List<T> b)
{
var newList = new List<T>(a);
newList.AddRange(b);
return newList;
}
public static bool Compare(this Customer customer1, Customer customer2)
{
if (customer1.CustomerId == customer2.CustomerId &&
customer1.Name == customer2.Name &&
customer1.City == customer2.City)
{
return true;
}
return false;
}
}
public class Customer
{
public int CustomerId { get; private set; }
public string Name { get; set; }
public string City { get; set; }
public Customer(int Id)
{
CustomerId = Id;
}
public override string ToString()
{
return Name + "/t" + City + "/t" + CustomerId;
}
}
class Program
{
static void Main(string[] args)
{
var customers = CreateCustomers();
var customerDictionary = new Dictionary<Customer, string>();
foreach (var c in customers)
customerDictionary.Add(c, c.Name.Split(' ')[1]);
var matches = customerDictionary.FilterBy(
(customer, lastName) => lastName.StartsWith("T"));
//上面执行查询操作
foreach (var m in matches)
Console.WriteLine(m);
}
public static List<Customer> FindCustomersByCity(
List<Customer> customers,
string city)
{
return customers.FindAll(c => c.City == city);
}
static List<Customer> CreateCustomers()
{
return new List<Customer>
{
new Customer(1) { Name = "Alex Roland", City = "Berlin" },
new Customer(2) { Name = "Oliver Cox", City = "Marseille" },
new Customer(3) { Name = "Maurice Taylor", City = "London" },
new Customer(4) { Name = "Phil Gibbins", City = "London" },
new Customer(5) { Name = "Tony Madigan", City = "Torino" },
new Customer(6) { Name = "Elizabeth A. Andersen", City = "Portland" },
new Customer(7) { Name = "Justin Thorp", City = "London" },
new Customer(8) { Name = "Bryn Paul Dunton", City = "Portland" }
};
}
}
}
本文来自: 本站内容欢迎转载,但是禁止去掉本文链接(转载无妨,去掉链接可耻!):http://www.ajaxcn.net/archives/183