Sample Files: Delegates Tutorial
Sample Files: Delegates Tutorial
Sample Files
See Delegates Sample to download and build the sample files
discussed in this tutorial.
Further Reading
● delegate
● 15. Delegates
● Events Tutorial
● Asynchronous Delegates
Tutorial
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. The delegate object can then be
passed to code which can call the referenced method, without having
to know at compile time which method will be invoked. Unlike function
pointers in C or C++, delegates are object-oriented, type-safe, and
secure.
A delegate declaration defines a type that encapsulates a method with
a particular set of arguments and return type. For static methods, a
delegate object encapsulates the method to be called. For instance
methods, a delegate object encapsulates both an instance and
a method on the instance. If you have a delegate object and an
appropriate set of arguments, you can invoke the delegate with the
arguments.
An interesting and useful property of a delegate is that it does not
know or care about the class of the object that it references. Any
object will do; all that matters is that the method's argument types
and return type match the delegate's. This makes delegates perfectly
suited for "anonymous" invocation.
Note Delegates run under the caller's security permissions, not the
declarer's permissions.
This tutorial includes two examples:
● Example 1 shows how to declare, instantiate, and call a delegate.
● Example 2 shows how to combine two delegates.
In addition, it discusses the following topics:
● Delegates and Events
● Delegates vs. Interfaces
Example 1
The following example illustrates declaring, instantiating, and
using a delegate. The BookDB class encapsulates a bookstore
database that maintains a database of books. It exposes a method
ProcessPaperbackBooks, which finds all paperback books in the
database and calls a delegate for each one. The delegate type used is
called ProcessBookDelegate. The Test class uses this class to print out
the titles and average price of the paperback books.
The use of delegates promotes good separation of functionality
between the bookstore database and the client code. The client code
has no knowledge of how the books are stored or how the bookstore
code finds paperback books. The bookstore code has no knowledge of
what processing is done on the paperback books after it finds them.
Copy
// bookstore.cs
using System;
Output
Copy
Paperback Book Titles:
The C Programming Language
The Unicode Standard 2.0
Dogbert's Clues for the Clueless
Average Paperback Book Price: $23.97
Code Discussion
● Declaring a delegate The following statement:
●Copy
●public delegate void ProcessBookDelegate(Book book);
Example 2
This example demonstrates composing delegates. A useful property of
delegate objects is that they can be composed using the "+" operator.
A composed delegate calls the two delegates it was composed from.
Only delegates of the same type can be composed.
The "-" operator can be used to remove a component delegate from a
composed delegate.
Copy
// compose.cs
using System;
class MyClass
{
public static void Hello(string s)
{
Console.WriteLine(" Hello, {0}!", s);
}