Delegate & Events in C# 详解






作为 C# 程序猿,Delegate 和 Events,其实每天都在用,从按键到各种网页都会用到,但是由于大多数时候,VS会自动生成其中的代码,很多人在写了几年之后,还是不了解其中的用法。这里简单的解说一下。

在解释events之前,先让我们解释一下delegate.

delegate 最早的概念来自与C, 用最简单的描述,delegate 相当与一个pointer,区别在于,pointer 指向一个赋值,delegate指向一个method.专业的解释 :

A delegate in C# is similar to a function pointer in C or C++. Using a delegate allows the programmer to encapsulate a reference to a method inside a delegate object. 继续阅读: %s




Understand Lambda Expressions






What is a Lambda Expression?

A lambda expression is an anonymous function that you can use to create delegates or expression tree types. By using lambda expressions, you can write local functions that can be passed as arguments or returned as the value of function calls. Lambda expressions are particularly helpful for writing LINQ query expressions.

(In versions of C# before 2.0, the only way to declare a delegate was to use named methods. C# 2.0 introduced anonymous methods and in C# 3.0 and later, lambda expressions supersede anonymous methods as the preferred way to write inline code) 继续阅读: %s