1. Trang chủ
  2. » Công Nghệ Thông Tin

Chapte r10 - Delegate, Events and Lambdas pdf

93 293 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 93
Dung lượng 421 KB

Nội dung

Chapter 10. Delegates,Events, and Lambdas Hoang Anh Viet VietHA@it-hut.edu.vn HaNoi University of Technology 1 MicrosoftMicrosoft 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.” MicrosoftMicrosoft 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 MicrosoftMicrosoft 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 MicrosoftMicrosoft 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); MicrosoftMicrosoft 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. MicrosoftMicrosoft 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. MicrosoftMicrosoft 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. MicrosoftMicrosoft 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 MicrosoftMicrosoft 10.2 Definding a Delegate in C#  Declare  Instantiate  and Use a Delegate [...]... In C# 1.0 and later, delegates can be declared as shown here: public delegate void Del(T item); public void Notify(int i) { } Del d1 = new Del(Notify);  In C# 2.0 and later, it is also possible to use an anonymous method to declare and initialize a delegate by using this simplified syntax: Del d2 = Notify; Microsoft Microsoft 10.2 Definding a Delegate in C#   In C# 3.0 and later,... private member variables (one for each delegate type) and two helper functions (OnAboutToBlow() and OnExploded()), that allow the client to add a method to the delegate’s invocation list Microsoft Microsoft Updating the Accelerate() method  we need to update the Accelerate() method to invoke each delegate, rather than iterate over an ArrayList of client-side sinks public void Accelerate(int delta) { //... d:"); d("D"); Console.Read(); 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 Microsoft Microsoft 16 10.3 The System.MulticastDelegate and System.Delegate Base class   The System.MulticastDelegate... MulticastDelegate : Delegate  Remarks • MulticastDelegate is a special class • Compilers and other tools can derive from this class • But we cannot derive from it explicitly Microsoft Microsoft Delegate Class   Represents a delegate, which is a data structure that refers to a static method or to a class instance and an instance method of that class Form: [SerializableAttribute] [ComVisibleAttribute(true)]... reference to the method's entry point and • A reference to a target object assignable to the type of the method's first argument  When the delegate is invoked, the first argument of the static method receives the target object 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... “match the pattern,”, receive a compile-time error public class SimpleMath { public static int SquareNumber(int a) { return a * a; } } // Error! Method does not match delegate pattern! BinaryOp b2 = new BinaryOp(SimpleMath.SquareNumber); 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... = Hello; Example (2) // Create the delegate object b that references // the method Goodbye: b = Goodbye; // The two delegates, a and b, are composed to form c: c = a + b; } } // Remove a from the composed delegate, leaving d, // which calls only the method Goodbye: d = c - a; System.Console.WriteLine("Invoking delegate a:"); a("A"); System.Console.WriteLine("Invoking delegate b:"); b("B"); System.Console.WriteLine("Invoking... currSpeed += delta; // Almost dead? if (10 == maxSpeed - currSpeed && almostDeadList != null) { almostDeadList("Careful buddy! Gonna blow!"); } // Still OK! if (currSpeed >= maxSpeed) carIsDead = true; else Console.WriteLine( "-> CurrSpeed = {0}", currSpeed); } } //end of Accelerate Remark    Before we invoke the methods maintained by the almostDeadList and explodedList member variables, we are checking... almostDeadList and explodedList member variables, we are checking them against a null value If the caller does not call the OnAboutToBlow() and OnExploded() helper methods, and we attempt to invoke the delegate’s invocation list, we will trigger a NullReferenceException and bomb at runtime Observe the updates to the Program class: Microsoft Microsoft ... instance of a delegate type that has references to: • An instance method of a type and a target object • • • Microsoft assignable to that type An instance method of a type, with the hidden this parameter exposed in the formal parameter list The delegate is said to be an open instance delegate A static method A static method and a target object assignable to the first parameter of the method The delegate . Chapter 10. Delegates ,Events, and Lambdas Hoang Anh Viet VietHA@it-hut.edu.vn HaNoi University of Technology 1 MicrosoftMicrosoft Objectives 2 “The purpose of Chapter 10 is to. Example 3 MicrosoftMicrosoft 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. 2008 lambda operator (=>) and exploring the connection between delegates, anonymous methods, and lambda expressions.” MicrosoftMicrosoft Roadmap 10.1 Understanding the .NET Delegate Type 10.2

Ngày đăng: 02/08/2014, 09:20

TỪ KHÓA LIÊN QUAN