Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Chapte r10 - Delegate, Events and Lambdas pdf
Nội dung xem thử
Mô tả chi tiết
Chapter 10.
Delegates,Events, and Lambdas
Hoang Anh Viet
HaNoi University of Technology
1
Microsoft Microsoft
Objectives
2
“The purpose of Chapter 10 is to demystify the delegate type.
Simply put, a .NET delegate is an object that “points” to other
methods in your application. Using this pattern, you are able to
build systems that allow multiple objects to engage in a two-way
onversation. After you have examined the use of .NET delegates,
you will then be introduced to the C# event keyword, which is
used to implify the manipulation of raw delegate programming.
You wrap up by investigating the role of the C# 2008 lambda
operator (=>) and exploring the connection between delegates,
anonymous methods, and lambda expressions.”
Microsoft Microsoft
Roadmap
10.1 Understanding the .NET Delegate Type
10.2 Defining a Delegate in C#
10.3 The System.MulticastDelegate and System.Delegate
Base class.
10.4 The Simplest Possible Delegate Example
10.5 Retrofiting the Car Type with Delegates
10.6 A more Elaborate Delegate Example
3
Microsoft Microsoft
Roadmap
10.7 Understanding Delegate Covarance
10.8 Greating Generic Delegates
10.9 Understanding C# Events.
10.10 The Generic C# EventHandler<T> Delegate.
10.11 Understanding C# Anonymouns Methods
10.12 Understanding Method Group Conversions
10.13 The C# 2008 Lambdas Operator.
4
Microsoft Microsoft
10.1 Understanding the .NET Delegate
Type
A delegate is a type that references a method.
Once a delegate is assigned a method, it behaves exactly
like that method .
The delegate method can be invoked like any other
method, with parameters and a return value.
Such as :
5
public delegate int PerformCalculation(int x, int y);
Microsoft Microsoft
Kinds of Methods
Any method from any accessible class or struct that
matches the delegate's signature, which consists of the
return type and parameters, can be assigned to the
delegate .
The method can be either static or an instance method .
Note :
• In the context of method overloading:
The signature of a method does not include the
return value.
• In the context of delegates:
The signature does include the return value.
Microsoft Microsoft
Delegates Overview
Delegates have the following properties:
• Delegates are like C++ function pointers but are type
safe.
• Delegates allow methods to be passed as
parameters.
• Delegates can be used to define callback methods.
• Delegates can be chained together; for example,
multiple methods can be called on a single event.
• Methods do not have to match the delegate signature
exactly
• Allow code blocks to be passed as parameters in
place of a separately defined method.
Microsoft Microsoft
Efficency of Delegates
Plug new code into existing classes.
Makes it possible to programmatically change method
calls.
Defines callback methods. Allows for an algorithm to be
written in a more general way.
Microsoft Microsoft
Roadmap
10.1 Understanding the .NET Delegate Type
10.2 Defining a Delegate in C#
10.3 The System.MulticastDelegate and System.Delegate
Base class.
10.4 The Simplest Possible Delegate Example
10.5 Retrofiting the Car Type with Delegates
10.6 A more Elaborate Delegate Example
9