在 2.0 之前的 C# 版本中,声明委托的唯一方法是使用命名方法。C# 2.0 引入了匿名方法,而在 C# 3.0 及更高版本中,Lambda 表达式取代了匿名方法,作为编写内联代码的首选方式

 // 声明一个委托
delegate void Printer(string s);
class TestClass
{
static void Main()
{
//lambda表达式
Printer pp = x => {
Console.WriteLine("print:" + x);
}; pp("sky");

//匿名方法的语法:delegate(){//里边是代码块}

// 摘要:
// Initializes a new instance of the System.Threading.Thread class.
//
// 参数:
// start:
// A System.Threading.ThreadStart delegate that represents the methods to be
// invoked when this thread begins executing.
//
// 异常:
// System.ArgumentNullException:
// The start parameter is null.
System.Threading.Thread t1 = new System.Threading.Thread
(delegate()
{
System.Console.Write("Hello, ");
System.Console.WriteLine("World!");
});
t1.Start(); // 匿名函数
Printer p = delegate(string j)
{
System.Console.WriteLine(j);
}; // Results from the anonymous delegate call:
p("The delegate using the anonymous method is called."); // 委托:将方法作为方法的参数
//下边将方法绑定到委托
//参考:张子阳的c#委托和事件http://www.cnblogs.com/JimmyZhang/archive/2007/09/23/903360.html
p = new Printer(TestClass.DoWork);
p += TestClass.DoWork1;
// p -= TestClass.DoWork;
//p -= TestClass.DoWork1;
// Results from the old style delegate call:
p("The delegate using the named method is called."); } // The method associated with the named delegate:
static void DoWork(string k)
{
System.Console.WriteLine(k);
} static void DoWork1(string k)
{
System.Console.WriteLine(k);
}
} //无参数的委托
 // 声明一个委托
delegate void del(); class TestClass
{
static void Main()
{
del myDelegate1 = () =>
{
Console.WriteLine("00xx");
};
myDelegate1(); System.Threading.Thread t1 = new System.Threading.Thread
(()=>
{
System.Console.Write("Hello, ");
System.Console.WriteLine("World!");
});
t1.Start(); t1 = new System.Threading.Thread
(delegate()
{
System.Console.Write("Hello, ");
System.Console.WriteLine("World!");
});
t1.Start();
}
}


MSDN上的委托例子:

下面的示例阐释声明、实例化和使用委托。BookDB 类封装一个书店数据库,它维护一个书籍数据库。它公开 ProcessPaperbackBooks 方法,该方法在数据库中查找所有平装书,并对每本平装书调用一个委托。使用的 delegate 类型名为 ProcessBookDelegate。Test 类使用该类打印平装书的书名和平均价格。

委托的使用促进了书店数据库和客户代码之间功能的良好分隔。客户代码不知道书籍的存储方式和书店代码查找平装书的方式。书店代码也不知道找到平装书后将对平装书执行什么处理。

// A set of classes for handling a bookstore:
namespace Bookstore
{
using System.Collections; // Describes a book in the book list:
public struct Book
{
public string Title; // Title of the book.
public string Author; // Author of the book.
public decimal Price; // Price of the book.
public bool Paperback; // Is it paperback? public Book(string title, string author, decimal price, bool paperBack)
{
Title = title;
Author = author;
Price = price;
Paperback = paperBack;
}
} // Declare a delegate type for processing a book:
public delegate void ProcessBookDelegate(Book book); // Maintains a book database.
public class BookDB
{
// List of all books in the database:
ArrayList list = new ArrayList(); // Add a book to the database:
public void AddBook(string title, string author, decimal price, bool paperBack)
{
list.Add(new Book(title, author, price, paperBack));
} // Call a passed-in delegate on each paperback book to process it:
public void ProcessPaperbackBooks(ProcessBookDelegate processBook)
{
foreach (Book b in list)
{
if (b.Paperback)
// Calling the delegate:
processBook(b);
}
}
}
} // Using the Bookstore classes:
namespace BookTestClient
{
using Bookstore; // Class to total and average prices of books:
class PriceTotaller
{
int countBooks = ;
decimal priceBooks = 0.0m; internal void AddBookToTotal(Book book)
{
countBooks += ;
priceBooks += book.Price;
} internal decimal AveragePrice()
{
return priceBooks / countBooks;
}
} // Class to test the book database:
class TestBookDB
{
// Print the title of the book.
static void PrintTitle(Book b)
{
System.Console.WriteLine(" {0}", b.Title);
} // Execution starts here.
static void Main()
{
BookDB bookDB = new BookDB(); // Initialize the database with some books:
AddBooks(bookDB); // Print all the titles of paperbacks:
System.Console.WriteLine("Paperback Book Titles:"); // Create a new delegate object associated with the static
// method Test.PrintTitle:
bookDB.ProcessPaperbackBooks(PrintTitle); // Get the average price of a paperback by using
// a PriceTotaller object:
PriceTotaller totaller = new PriceTotaller(); // Create a new delegate object associated with the nonstatic
// method AddBookToTotal on the object totaller:
bookDB.ProcessPaperbackBooks(totaller.AddBookToTotal); System.Console.WriteLine("Average Paperback Book Price: ${0:#.##}",
totaller.AveragePrice());
} // Initialize the book database with some test books:
static void AddBooks(BookDB bookDB)
{
bookDB.AddBook("The C Programming Language", "Brian W. Kernighan and Dennis M. Ritchie", 19.95m, true);
bookDB.AddBook("The Unicode Standard 2.0", "The Unicode Consortium", 39.95m, true);
bookDB.AddBook("The MS-DOS Encyclopedia", "Ray Duncan", 129.95m, false);
bookDB.AddBook("Dogbert's Clues for the Clueless", "Scott Adams", 12.00m, true);
}
}
}

输出:

Paperback Book Titles:
The C Programming Language
The Unicode Standard 2.0
Dogbert's Clues for the Clueless
Average Paperback Book Price: $23.97

声明委托

public delegate void ProcessBookDelegate(Book book);

实例化委托。

声明了委托类型后,必须创建委托对象并使之与特定方法关联。在上一个示例中,您通过按下面示例中的方式将 PrintTitle 方法传递到 ProcessPaperbackBooks方法来实现这一点:

bookDB.ProcessPaperbackBooks(PrintTitle);

这将创建与静态方法 Test.PrintTitle 关联的新委托对象。类似地,对象 totaller 的非静态方法 AddBookToTotal 是按下面示例中的方式传递的:

bookDB.ProcessPaperbackBooks(totaller.AddBookToTotal);

在两个示例中,都向 ProcessPaperbackBooks 方法传递了一个新的委托对象。

委托创建后,它的关联方法就不能更改;委托对象是不可变的。

调用委托。

创建委托对象后,通常将委托对象传递给将调用该委托的其他代码。通过委托对象的名称(后面跟着要传递给委托的参数,括在括号内)调用委托对象。下面是委托调用的示例:

processBook(b);

与本例一样,可以通过使用 BeginInvokeEndInvoke 方法同步或异步调用委托。

在C#中使用线程的方法很多,使用委托的BeginInvoke和EndInvoke方法就是其中之一。BeginInvoke方法可以使用线程异步地执行委托所指向的方法。然后通过EndInvoke方法获得方法的返回值(EndInvoke方法的返回值就是被调用方法的返回值),或是确定方法已经被成功调用。我们可以通过四种方法从EndInvoke方法来获得返回值。

详情:这里

分析:

看起来 是不是很像接口啊 给后来着留一个委托 关注返回值和参数

后来者实现委托 内部可以随便定义东东 但是 返回值和参数必须和委托的一样

后来者想咋实现就咋实现

何时使用委托何时使用接口呢 来自MSDN:

委托和接口都允许类设计器分离类型声明和实现。任何类或结构都能继承和实现给定的接口。可以为任何类上的方法创建委托,前提是该方法符合委托的方法签名。接口引用或委托可由不了解实现该接口或委托方法的类的对象使用。既然存在这些相似性,那么类设计器何时应使用委托,何时又该使用接口呢?

在以下情况下,请使用委托:

当使用事件设计模式时。

当封装静态方法可取时。

当调用方不需要访问实现该方法的对象中的其他属性、方法或接口时。

需要方便的组合。

当类可能需要该方法的多个实现时。

在以下情况下,请使用接口:

当存在一组可能被调用的相关方法时。

当类只需要方法的单个实现时。

当使用接口的类想要将该接口强制转换为其他接口或类类型时。

当正在实现的方法链接到类的类型或标识时:例如比较方法。

使用单一方法接口而不使用委托的一个很好的示例是 IComparable 或泛型版本 IComparable<(Of <(T>)>)。IComparable 声明 CompareTo 方法,该方法返回一个整数,指定相同类型的两个对象之间的小于、等于或大于关系。IComparable 可用作排序算法的基础。虽然将委托比较方法用作排序算法的基础是有效的,但是并不理想。因为进行比较的能力属于类,而比较算法不会在运行时改变,所以单一方法接口是理想的

3 委托、匿名函数、lambda表达式的更多相关文章

  1. (28)C#委托,匿名函数,lambda表达式,事件

    一.委托 委托是一种用于封装命名和匿名方法的引用类型. 把方法当参数,传给另一个方法(这么说好理解,但实际上方法不能当参数,传入的是委托类型),委托是一种引用类型,委托里包含很多方法的引用 创建的方法 ...

  2. 【Unity|C#】基础篇(9)——匿名函数 / Lambda表达式

    [学习资料] <C#图解教程>(第13章):https://www.cnblogs.com/moonache/p/7687551.html 电子书下载:https://pan.baidu. ...

  3. 委托-异步调用-泛型委托-匿名方法-Lambda表达式-事件【转】

    1. 委托 From: http://www.cnblogs.com/daxnet/archive/2008/11/08/1687014.html 类是对象的抽象,而委托则可以看成是函数的抽象.一个委 ...

  4. 匿名函数 lambda表达式(lambda expression)

    阅读g2log时,发现有两行代码居然看不懂. 1. auto bg_call =  [this, log_directory]() {return pimpl_->backgroundChang ...

  5. C#多线程+委托+匿名方法+Lambda表达式

    线程 下面是百度写的: 定义英文:Thread每个正在系统上运行的程序都是一个进程.每个进程包含一到多个线程.进程也可能是整个程序或者是部分程序的动态执行.线程是一组指令的集合,或者是程序的特殊段,它 ...

  6. Qt中使用匿名函数lambda表达式

    一.为什么要使用匿名函数lamdba 首先,lambda表达式可以使代码变得简单,C++中,一个lambda表达式表示一个可调用的代码单元.如代码: #include <QCoreApplica ...

  7. Python匿名函数——lambda表达式

    如果要定义的函数很简单,一个return语句就能搞定,可以使用lambda表达式来定义, lambda表达式的语法如下: lambda parameters: expression lambda表达式 ...

  8. python做中学(八)匿名函数lambda的用法

    匿名函数,顾名思义即没有名称的函数,和def定义的函数的最大区别在于匿名函数创建后返回函数本身(即匿名函数不需要return来返回值),表达式本身结果就是返回值,而def创建后则赋值给一个变量名,在P ...

  9. 匿名函数 =匿名方法+ lambda 表达式

    匿名函数的定义和用途 匿名函数是一个"内联"语句或表达式,可在需要委托类型的任何地方使用. 可以使用匿名函数来初始化命名委托[无需取名字的委托],或传递命名委托(而不是命名委托类型 ...

  10. C# delegate event func action 匿名方法 lambda表达式

    delegate event action func 匿名方法 lambda表达式 delegate类似c++的函数指针,但是是类型安全的,可以指向多个函数, public delegate void ...

随机推荐

  1. Python 实现网络爬虫小程序

    Python很简洁,也很强大,作为兴趣,值得一学!   下面这个程序实现的是从一个网站上下载图片,根据自己需要可以进行修改 import re import urllib def gethtml(ur ...

  2. ODAC with Oracle Developer Tools for Visual Studio

    c#开发Oracle数据库的时候,需要本机没有安装过 Oracle 客户端,直接下载 ODAC with Oracle Developer Tools for Visual Studio工具安装即可 ...

  3. PCB的技巧

    (1)首先元件的移动,如下图中,向让D1的2引脚和R49的2引脚齐平,但是移动的距离每次都超过,不能平齐 修改元件最小移动距离即可,如下图,其中有很多可以改动的地方,但是需要改的是Component ...

  4. CodeFirst-数据迁移-Migration

    http://www.cnblogs.com/haogj/archive/2012/02/17/2356537.html 1.安装最新NuGet 2.安装EntityFramework:在程序包管理器 ...

  5. Python的函数式编程-传入函数、排序算法、函数作为返回值、匿名函数、偏函数、装饰器

    函数是Python内建支持的一种封装,我们通过把大段代码拆成函数,通过一层一层的函数调用,就可以把复杂任务分解成简单的任务,这种分解可以称之为面向过程的程序设计.函数就是面向过程的程序设计的基本单元. ...

  6. Area - POJ 1265(pick定理求格点数+求多边形面积)

    题目大意:以原点为起点然后每次增加一个x,y的值,求出来最后在多边形边上的点有多少个,内部的点有多少个,多边形的面积是多少. 分析: 1.以格子点为顶点的线段,覆盖的点的个数为GCD(dx,dy),其 ...

  7. pom.xml第一行报错

    错误:Cannot detect Web Project version. Please specify version of Web Project through <version> ...

  8. tomcat报错:This is very likely to create a memory leak问题解决

    tomcat memory leak解决方案 这种问题在开发中经常会碰到的,看看前辈的总结经验 Tomcat内存溢出的原因  在生产环境中tomcat内存设置不好很容易出现内存溢出.造成内存溢出是不一 ...

  9. Jsp中的EL表达式

    EL表达式作用: 向浏览器输出域对象中的变量值或表达式计算的结果!!! 语法: ${变量或表达式} 可以通过page指令来设置EL表示是否启用,false是不启用,true是启用,默认是true &l ...

  10. web安全培训笔记

    1.漏洞获取方法 1)扫描器扫描 2)乌云 3)线上服务漏洞 样例: 线上crm->管理员弱口令->后台上传头像处漏洞->上传php文件->进入内网->扫描内网拓扑-&g ...