70-536 exam: Delegate functions
23 March 2010
The concepts behind delegates are advanced. The following relatively simple example from Wikipedia shows how a delegate method works
A delegate is a form of type-safe function pointer used by the .NET Framework. Delegates specify a method to call and optionally an object to call the method on. They are used, among other things, to implement callbacks and event listeners. It encapsulates 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.
C# Code Example
Code to define a delegate:
delegate void SendMessageDelegate(Message message);
Code to define a method which takes an instantiated delegate as its argument:
void SendMessage(SendMessageDelegate sendMessageDelegateReference)
{
// call the delegate and any other chained delegates synchronously
sendMessageDelegateReference(new Message("hello this is a message"));
}
The implemented method which runs when the delegate is called:
void HandleSendMessage(Message message)
{
// the implementation for the Sender and Message classes are not relevant to this example
Sender.Send(message);
}
Code to call the SendMessage method, passing an instantiated delegate as an argument:
SendMessage(new SendMessageDelegate(HandleSendMessage));
Source: Wikipedia
There also appears to be some deep rooted architectural contention between Sun and Microsoft here:
The Truth About Delegates Sun Microsystems recently released a white paper that derides delegates as "unnecessary", "detrimental to the language", "harmful", "complex", and "not object-oriented." We strongly disagree with Sun’s technical arguments. 1.1 Delegates dynamic advantages Sun’s white paper identifies the need for "some equivalent of method pointers" to support "pluggable" APIs: It has been clear from the outset that the original language needed some equivalent of method pointers in order to support delegation and "pluggable" APIs. James Gosling noted this in his keynote at the first JavaOne conference. We agree with this statement, and believe that a delegate-based event model is exactly the solution to this problem. In fact, we believe that JavaBeans’ interface-basedevent model precludes JavaBeans from being "pluggable" in some important scenarios...
Source: Microsoft
Fortunately for the 70-536 exam we need to understand Microsoft. However you should be able to follow the more basic examples:
Example 1
The following is a simple example of declaring and using a delegate.
// keyword_delegate.cs // delegate declaration delegate void MyDelegate(int i); step one class Program { public static void Main() { TakesADelegate(new MyDelegate(DelegateFunction)); step two } public static void TakesADelegate(MyDelegate SomeFunction) { SomeFunction(21); step three } public static void DelegateFunction(int i) { System.Console.WriteLine("Called by delegate with number: {0}.", i); step four } }
Output:
Called by delegate with number: 21.
Stepping through example 1:
- declare a delegate method called MyDelegate (returning void and taking an int as a parameter)
- in the Main method we call TakesADelegate passing as a parameter a new anonymous instance of MyDelegate(DelegateFunction) i.e. the clearly defined DelegateFunction(int) function is pass via MyDelegate.
- within the TakesADelegate method, it accepts the DelegateFunction, here referred to by the variable SomeFunction and then pass it a parameter of 21
- in DelegateFunction(21) – simple console output
Example 2
In the following example, one delegate is mapped to both static and instance methods and returns specific information from each.
// keyword_delegate2.cs // Calling both static and instance methods from delegates using System; // delegate declaration delegate void MyDelegate(); step one – declare MyDelegate public class MyClass step two – MyClass with instance and static methods { public void InstanceMethod() { Console.WriteLine("A message from the instance method."); } static public void StaticMethod() { Console.WriteLine("A message from the static method."); } } public class MainClass { static public void Main() { MyClass p = new MyClass(); // Map the delegate to the instance method: MyDelegate d = new MyDelegate(p.InstanceMethod); step 3a point MyDelegate at InstanceMethod d(); step 3b call/test MyDelegate // Map to the static method: d = new MyDelegate(MyClass.StaticMethod); step 4a point MyDelegate at StaticMethod d(); step 4b call/test MyDelegate } }
Output:
A message from the instance method. A message from the static method.
Stepping through example 2:
- declare a delegate method called MyDelegate (returning void and taking an int as a parameter)
- setup MyClass with instance and static methods
- assign d (of type MyDelegate) and assign it the “MyClass Instance Method (here p is the instance of MyClass)” – then call/ test output
- assign d (of type MyDelegate) and assign it the “MyClass Static Method” – then call/ test output
If you liked this blog post, you may want to subscribe to my news feed in your RSS reader.
