Abstract
A delegate is a user-defined type, like a class. But whereas a class represents a collection of data, a delegate keeps track of one or more methods. You use a delegate by doing the following. We’ll go through each of these steps in detail in the following sections.
-
1.
Declare a new delegate type with a particular signature and return type. A delegate declaration looks like a method declaration, except that it doesn’t have an implementation block.
-
2.
Declare a delegate variable of the new delegate type.
-
3.
Create an object of the delegate type, and assign it to the delegate variable. The new delegate object includes a reference to a method with the same signature as defined in the first step.
-
4.
Add additional methods into the delegate object. These methods must have the same signature and return type as the delegate type defined in the first step.
-
5.
Throughout your code you can then invoke the delegate, just as it if it were a method. When you invoke the delegate, each of the methods it contains is executed.
Access this chapter
Tax calculation will be finalised at checkout
Purchases are for personal use only
Preview
Unable to display preview. Download preview PDF.
Rights and permissions
Copyright information
© 2010 Daniel M. Solis
About this chapter
Cite this chapter
Solis, D.M. (2010). Delegates. In: Illustrated C# 2010. Apress. https://doi.org/10.1007/978-1-4302-3283-4_15
Download citation
DOI: https://doi.org/10.1007/978-1-4302-3283-4_15
Publisher Name: Apress
Print ISBN: 978-1-4302-3282-7
Online ISBN: 978-1-4302-3283-4
eBook Packages: Professional and Applied ComputingApress Access BooksProfessional and Applied Computing (R0)