Microsoft® Visual C#® 2012 Step by Step

Microsoft® Visual C#® 2012 Step by Step

Author:John Sharp [John Sharp]
Language: eng
Format: epub
Tags: COMPUTERS / Programming Languages / C#
ISBN: 9780735667990
Publisher: Microsoft Press
Published: 2012-12-21T16:00:00+00:00


Understanding Delegates

A delegate is a reference to a method. It is a very simple concept with extraordinarily powerful implications. Let me explain.

Note

Delegates are so-named because they “delegate” processing to the referenced method when they are invoked.

Typically, when you write a statement that invokes a method, you specify the name of the method (and possibly specify the object or structure to which the method belongs). It is clear from your code exactly which method you are running and when you are running it. Look at the following simple example that calls the performCalculation method of a Processor object (what this method does or how the Processor class is defined is immaterial for this discussion):

Processor p = new Processor(); p.performCalculation();

A delegate is an object refers to a method. You can assign a reference to a method to a delegate in much the same way that you can assign an int value to an int variable. The next example creates a delegate named performCalculationDelegate that references the performCalculation method of the Processor object. I have deliberately omitted some elements of the statement that declares the delegate, as it is important to understand the concept rather than worry about the syntax (you will see the full syntax shortly):

Processor p = new Processor(); delegate ... performCalculationDelegate ...; performCalculationDelegate = p.performCalculation;

It is important to understand that the statement that assigns the method reference to the delegate does not run the method at this point; there are no parentheses after the method name, and you do not specify any parameters (if the method takes them). This is just an assignment statement.

Having stored a reference to the performCalculation method of the Processor object in the delegate, the application can subsequently invoke the method through the delegate, like this:

performCalculationDelegate();

This looks like an ordinary method call; if you did not know otherwise, it looks like you might actually be running a method named performCalculationDelegate. However, the common language runtime (CLR) knows that this is a delegate, so it retrieves the method that the delegate references and runs that instead. Later on, you can change the method that a delegate refers to, so a statement that calls a delegate might actually run a different method each time it executes. Additionally, a delegate can reference more than one method at a time (think of it as a collection of method references), and when you invoke a delegate all of the methods that it refers to will run.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.