Deep C# - Delegates |
Written by Mike James | |||||
Monday, 02 June 2025 | |||||
Page 2 of 4
The C# ApproachThe overall C# approach to allowing functions to be first-class entities has developed over time as well. Initially delegates were used as a way of wrapping a function in an object in a way that is superficially similar to Java's approach but with some special advantages. C# delegates also acquired features that made them easier to use - anonymous methods and finally lambdas. However, it is important that you fully understand the original idea of delegates and how to use them, so let’s look at how it works and some of the more interesting ways that you can put it to work. The relationship of delegates to events is covered in the next chapter. Delegate BasicsWhat is initially confusing is that to create a delegate you first have to create a type and then create an instance of the type. That is, a delegate is a user-defined reference type that encapsulates a method. Consider, for example, how to encapsulate the simple example method:
All this does is display the current value of First, we need to define a delegate type that matches its signature, including the return type:
This delegate type defines the methods that it can encapsulate. Notice that it is only the function’s signature and return type that specifies the delegate type. The Next, we have to create an instance of the type and supply it with the
If you prefer you can use the overloaded assignment operator:
to create an instance. Now we can run the original
or by using the delegate’s invoke method:
or by calling the delegate instance as if it was the
This last form is just a convenience as it implicitly uses the Notice that the default use of the Of course, there are a few slightly hidden details in these examples which are obvious to experts, but often confuse the beginner. The first is that the method being wrapped is still a method belonging to some object and not a "disembodied" function. When the delegate is used to wrap the method the method has to be accessible from wherever the delegate is being declared. In the example above there is the implicit assumption that all of the code is defined within the same class. A more elaborate and complete example would create the
and then within the class that wants to make use of the method you can make use of the delegate type to wrap the method instance:
Another subtle point is that To summarize:To wrap a method belonging to an instance of a class you have to first define a new delegate type with a specific signature and return type, then you wrap the method in an instance of the delegate type. |
|||||
Last Updated ( Monday, 02 June 2025 ) |