Events and delegates

  • Thread starter Thread starter Chak
  • Start date Start date
C

Chak

Can somebody explain to a blockhead like me as to what is the difference
between a delegate and an event in .NET ?
 
Essentially, an event is a type of implementation of a delegate. Rather than
me type a novel, take a look at this:

http://www.codeproject.com/csharp/delegates-part1.asp

(a simpler explanation here, but not as much fun:
http://www.codeproject.com/csharp/DelegatesAndEvents.asp)

The delegate is far more flexible and has better reuse, as the event
constrains the implementation quite a bit.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 
This is not an easy question to answer, blockhead or no!

Events and Delegates are related, in that events use delegates as event
handlers.

A Delegate is similar to a function pointer. The delegate itself defines a
signature that any method which is used as that delegate has the correct
signature (type, parameters, etc). The delegate itself defines no code. It
is not meant to be used directly, but to define a type of method that will
be used in its place. Delegates are handy when you want to handle the same
situation in different ways. A good example is the overload for
Regex.Replace(...) that is defined as follows:

public string Regex.Replace(string input, MatchEvaluator evaluator)

The System.Text.RegularExpressions.MatchEvaluator is a delegate, defined as
such:

public delegate string MatchEvaluator (Match match);

It defines a method signature for a method that takes a
System.Text.RegularExpressions.Match class instance as a parameter and
returns a string. What it does is not defined. What string it returns is not
defined. For example, you could write a method like the following that could
be used in its place:

public string evaluator(Match match)
{
return "foo";
}

Now, the purpose of this delegate is this: The method delegate is passed to
the Regex.Replace() function. The Regex.Replace() function then gets all the
matches of the input string, and calls the MatchEvaluator delegate for each
one, replacing the matched text with the return value of the delegate
method. It then returns the replaced string.

to use it, you literally pass your function to it, using the delegate
Contructor, as in the following:

public string evaluator(Match m)
{
return "foo";
}

public string ReplaceFoo()
{
Regex r = new Regex("O"); // "O" is the Regular Expression
string inputString = "XXXXOOOO";
return r.Replace(inputString, new MatchEvaluator(evaluator);
}

The return value would look like this:

"XXXXfoofoo"

Now, when it comes to Events, you have basically the same deal. the
EventHandler class is a delegate, as are all of its descendants. So, when
you raise an event, you define an EventHandler that handles it. Example:

public event EventHandler Closing;
protected void RaiseClosing(EventArgs e)
{
if (Closing != null) Closing(this, e);
}

The event is "Closing", which is a delegate of type EventHandler. The
EventHandler is defined by the type of the event Closing, which is a type of
EventHandler. The EventHandler delegate looks like the following:

public delegate void EventHandler(Object sender, EventArgs e);

The RaiseClosing method takes an EventArgs class and calls the delegate,
using "this" (the object itself) as the first argument, and the EventArgs
class passed to it as the second.

Now, when this class wants to raise the event, it calls RaiseClosing, like
so:

RaiseClosing(new EventArgs());

When this, or any other class wants to handle the event, it defines the
event handler delegate, thusly:

protected void OnClosing(object sender, EventArgs e)
{
// do something
}

Then it defines this method as the delegate itself (in this case, the class
is handling its own event):

this.Closing += new EventHandler(this.OnClosing);

So, the "OnClosing" method becomes the "Closing" delegate, and is called by
the "RaiseClosing" method, which passes the EventArgs that is passed to it
to the "Closing" delegate ("OnClosing");

One more thing you may be wondering about: What is that "+=" stuff, anyway?
Well, to add to the confusion, .Net delegates are "multicast" delegates. You
can define as many method instances to them as you want, by appending them
to it. Each delegate method declaration is executed in the order in which it
is added. This means that, in the case of Events (at least!) you can have as
many methods in as many classes as you like handle the same event being
raised by the same class instance.

Now, go back and re-read this 3 or 4 times, and it should eventually make
sense to you!

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.
 
Back
Top