With different .NET frameworks coming to existence, the usage of delegates becomes more and more desirable.
To satisfy the needs .NET 3.5 introduced the whole new world of delegates.
Below I present a table with delegates’ names and descriptions which comes with .NET3.5 framework (.NET 4.0 has even more built-in delegates).
In order to be used a delegate first must be declared. During the declaration one defines a signature of the function that delegate may represent.
By having those predefined delegates we can skip the declaration part.
.NET offered not only a set of such convenient delegates but also it embedded Generic data type to many of them.
As you can see many delegates have Generic type as a parameter or/and return value.
It is very convenient. You can substitute the Generic type by any type in your code and the compiler will do the rest.
Delegate Description
Action Encapsulates a method that takes no parameters and does not return a value.
Action
Action
Action
Action
AppDomainInitializer Represents the callback method to invoke when the application domain is initialized.
AssemblyLoadEventHandler Represents the method that handles the AssemblyLoad event of an AppDomain.
AsyncCallback References a method to be called when a corresponding asynchronous operation completes.
Comparison
ConsoleCancelEventHandler Represents the method that will handle the CancelKeyPress event of a System.Console.
Converter
CrossAppDomainDelegate Used by DoCallBack for cross-application domain calls.
EventHandler Represents the method that will handle an event that has no event data.
EventHandler
Func
Func
Func
Func
Func
Predicate
ResolveEventHandler Represents the method that handles the TypeResolve, ResourceResolve, and Assembly Resolve events of an AppDomain.
UnhandledExceptionEventHandler Represents the method that will handle the event raised by an exception that is not handled by the application domain.
In this article I want to discuss different technique to deal with custom and pre-defined delegates.
There are many ways to skin a cat. I don’t like this saying, because I have two cats at home and I love them.
But there are many different ways how to use a delegate in your program for sure.
We can sort out the delegates from table.
1. Generic type functions and methods
* Action
* Func
* Converter
* Comparison
* Predicate
2. Event handlers
* AssemblyLoadEventHandler
* ConsoleCancelEventHandler
* EventHandler
* EventHandler
* ResolveEventHandler
* UnhandledExceptionEventHandler
3. Others
* AppDomainInitializer
* AsyncCallback (we are already familiar with this delegate – Part2)
* CrossAppDomainDelegate
My intention is mostly talk about the first three delegates Action, Func, Converter.
I want to demonstrate how to use these delegates in different ways.
Eventually the compiler would generate pretty similar code, but from programming stand point this technique presents great difference.
Wednesday, October 13, 2010
Delegates in C#
Subscribe to:
Post Comments (Atom)






0 comments:
Post a Comment