“Lambda 表达式”(lambda expression)是一个匿名函数

省略delegate,甚至省略参数类型;

直接用(参数)=> {语句或表达式}

例如:

button1.Click += (sender, e) =>{......}

new Thread  ( () => {......} ). Start();

PlotFun ( x => x*x, ,  );

lambda特点:

lambda表达式比匿名函数简单,不写参数的匿名函数可以转化成任意多个参数的委托。

使用示例:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; using System.Threading; namespace MethodDelegateLamda
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} //示例1,使用线程
private void button1_Click(object sender, EventArgs e)
{
//csharp 1.0
//使用委托,使用已定义好的函数
new Thread(new ThreadStart(MyFun)).Start(); //csharp 2.0
//省略委托:MyFun自动实例化为ThreadStart委托(
new Thread(MyFun).Start();
//匿名方法
new Thread(new ThreadStart( delegate(){ Console.Write("my function"); })).Start();
//匿名方法,省略参数列表
new Thread(new ThreadStart( delegate{ Console.Write("my function"); })).Start();
//匿名方法,自动转委托
new Thread( delegate(){ Console.Write("my function"); }).Start(); //csharp 3.0
//Lambda表达式
new Thread(() => { Console.Write("my function"); }).Start(); } void MyFun()
{
Console.Write("my function");
} //示例2,使用事件 private void button3_Click(object sender, EventArgs e)
{
Example3();
}
void Example3()
{
//csharp 1.0
//使用委托,使用自定义函数
this.MouseMove += new MouseEventHandler(Form1_MouseMove); //csharp 2.0
//自动转委托
this.MouseMove += Form1_MouseMove; Label lbl = this.label1; //这个变量下面使用了闭包(简单地说,使用外部的局部变量)
this.MouseMove += new MouseEventHandler(delegate(object sender, MouseEventArgs e) { lbl.Text = e.X + "," + e.Y; });
this.MouseMove += delegate(object sender, MouseEventArgs e) { lbl.Text = e.X + "," + e.Y; }; //csharp 3.0
//使用Lambda表达式
this.MouseMove += (object sender, MouseEventArgs e) => { lbl.Text = e.X + "," + e.Y; }; //Lamda
this.MouseMove += (sender, e) => { lbl.Text = e.X + "," + e.Y; }; //可以省略类型
} void Form1_MouseMove(object sender, MouseEventArgs e)
{
this.label1.Text = e.X + "," + e.Y;
} //示例3, 数组排序
class Book
{
public string title;
public double price;
public Book(string title, double price) { this.title=title; this.price=price; }
}
private void button2_Click(object sender, EventArgs e)
{
Random rnd = new Random();
Book [] books = new Book[ ];
for( int i=; i<books.Length; i++ ) books[i] = new Book( "Book"+i, rnd.Next() ); //csharp 1.0
Array.Sort(books, new MyComparer()); //用一个IComparer //csharp 2.0
Array.Sort<Book>(books, new Comparison<Book>(delegate(Book book1, Book book2) { return (int)(book1.price - book2.price); })); //使用Comparison委托
Array.Sort<Book>(books, delegate(Book book1, Book book2) { return (int)(book1.price - book2.price); }); //csharp 3.0
Array.Sort<Book>(books, (Book book1, Book book2) => (int)(book1.price - book2.price));
Array.Sort<Book>(books, (book1, book2) => (int)(book1.price - book2.price)); //省略参数类型 //使用Linq
IOrderedEnumerable<Book> result = from book in books orderby book.price select book; var result2 = from book in books where book.price>= orderby book.price select book.title;
foreach (string s in result2) Console.Write(s); var result3 = books
.Where<Book>(b => b.price>=)
.OrderBy<Book, double>(b => b.price, Comparer<double>.Default)
.Select<Book,Book>(book => book);
foreach (Book b in result3) Console.Write(b.price+" ");
} class MyComparer : System.Collections.IComparer
{
public int Compare(object x1, object x2)
{
return (int)(((Book)x1).price - ((Book)x2).price);
}
}
}
}

C#高级特性_Lambda的更多相关文章

  1. ActiveMQ中的Destination高级特性(一)

    ---------------------------------------------------------------------------------------- Destination ...

  2. Python3学习(二)-递归函数、高级特性、切片

    ##import sys ##sys.setrecursionlimit(1000) ###关键字参数(**关键字参数名) ###与可变参数不同的是,关键字参数可以在调用函数时,传入带有参数名的参数, ...

  3. 云端卫士实战录 | Java高级特性之多线程

    <实战录>导语 一转眼作为一名Java开发者已经四年多时间了,说长不长说短不短,对于java的感情还是比较深的,主要嘛毕竟它给了我饭吃.哈哈,开个玩笑.今天我想借此机会来和大家聊聊Java ...

  4. javascript高级特性

    01_javascript相关内容02_函数_Arguments对象03_函数_变量的作用域04_函数_特殊函数05_闭包_作用域链&闭包06_闭包_循环中的闭包07_对象_定义普通对象08_ ...

  5. Visual Studio 2015 速递(4)——高级特性之移动开发

    系列文章 Visual Studio 2015速递(1)——C#6.0新特性怎么用 Visual Studio 2015速递(2)——提升效率和质量(VS2015核心竞争力) Visual Studi ...

  6. Android TextView高级特性使用

    TextView一般都是用来显示一段文本,这里说的高级特性主要是一些我们平常不太常用的属性.包括文字阴影.自定义字体.html嵌入多格式.字体加粗.插入图片.这些特性平时开发APP的时候,可能一般使用 ...

  7. Python的高级特性8:你真的了解类,对象,实例,方法吗

    Python的高级特性1-7系列是本人从Python2过渡3时写下的一些个人见解(不敢说一定对),接下来的系列主要会以类级为主. 类,对象,实例,方法是几个面向对象的几个基本概念,其实我觉得很多人并不 ...

  8. Python的高级特性7:闭包和装饰器

    本节跟第三节关系密切,最好放在一起来看:python的高级特性3:神奇的__call__与返回函数 一.闭包:闭包不好解释,只能先看下面这个例子: In [23]: def outer(part1): ...

  9. VQuery高级特性

    VQuery高级特性 css方法 同时设置多个--for in 链式操作 链式操作 函数,链式操作 css 方法链式操作 json的使用 阻止冒泡,默认事件 VQuery插件 插件机制 可以扩展库的功 ...

随机推荐

  1. oracle之报错:ORA-00054: 资源正忙,要求指定 NOWAIT

    oracle之报错:ORA-00054: 资源正忙,要求指定 NOWAIT 问题如下: SQL> conn scott/tiger@vm_database Connected to Oracle ...

  2. Windows Azure虚拟机和云服务实例计费方式更新

    在之前的Windows Azure计费账单中,A0,A1,A2,A3,A4系列的虚拟机(云服务实例)都是以A1为基准计费单位的,即: 虚拟机大小 计费单位(小时) A0 A1*0.25 A1 A1*1 ...

  3. 证明你是你——快速开启Windows Azure多重身份验证

    中国版Windows Azure的多重身份验证(Multi-Factor Authentication)功能已经开放.这个功能说白了就是要“证明你是你”.目前可以支持以下几种验证方式: 手机,短信验证 ...

  4. high三个晚上这样好么-JSON&PHP

    hi 昨晚上吃火锅去了,对,你没猜错,我就是在成都 今晚有师兄请客,明天有基友请吃火锅,本来该忙忙哒的这一周要连续high三个晚上么(单身研究生狗就是这么容易满足).所以只好不务正业写写写了(写不动了 ...

  5. java utf-8文件处理bom头

    UTF? UTF,是UnicodeTransformationFormat的缩写,意为Unicode转换格式. 即怎样将Unicode定义的数字转换成程序数据.utf是对Unicode的一种编码格式化 ...

  6. stanford coursera 机器学习编程作业 exercise4--使用BP算法训练神经网络以识别阿拉伯数字(0-9)

    在这篇文章中,会实现一个BP(backpropagation)算法,并将之应用到手写的阿拉伯数字(0-9)的自动识别上. 训练数据集(training set)如下:一共有5000个训练实例(trai ...

  7. c语言结构体小知识

    引自:http://c.biancheng.net/cpp/html/88.html 结构体在内存中是连续存储的 struct stu{ char *name; //姓名 int num; //学号 ...

  8. BZOJ4152The Captain[DIjkstra]

    4152: [AMPPZ2014]The Captain Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 700  Solved: 266[Submit ...

  9. NOIP2005过河[DP 状态压缩]

    题目描述 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子上.由于桥的长度和青蛙一次跳过的距离都是正整数,我们可以把独木桥上青蛙可能到达的点看成数 ...

  10. js对象定义

    JS中的对象定义方式,跟服务端,还是有很大差别的! 现在来说一下JS类的定义 工厂模式 function creatHeven(name,age){ var temp =new Object(); t ...