Delegates: When and why (besides events and threads)?

  • Thread starter Thread starter Jon Davis
  • Start date Start date
J

Jon Davis

I've used delegates fairly heavily for several years in C# for event
handling and for starting threads.

Does anyone have any real-world scenarios where delegates were both
extremely useful and extremely appropriate (as opposed to other programmatic
means or mechanisms) that were NOT related to events and ThreadStarts?

Thanks,
Jon
 
Jon Davis said:
I've used delegates fairly heavily for several years in C# for event
handling and for starting threads.

Does anyone have any real-world scenarios where delegates were both
extremely useful and extremely appropriate (as opposed to other
programmatic means or mechanisms) that were NOT related to events and
ThreadStarts?

I have an imaging app that can capture images from multiple sources, I use a
delegate so that whatever code is capturing the image can send new images,

eg
CaptureFromScanner(new CallbackDelegate(this.ImageCaptured));
one of the methods CaptureFromClipboard doesn't have a form or any object
associated with it so can't raise events.

A second example, I define a status delegate like this:

public delegate void StatusDelegate(int PercentComplete, ref bool Cancel);

that is used to provide a progress bar for a range of functions. Again these
function could be in static methods so can't do events.

Michael
 
Sure. They are all delegates/callback, just presented in different ways.
Most classes that expose Beginxxx async methods, take a callback delegate.
You can pass delegates your own classes (i.e. like Thread does) instead of
exposing public events if, for example, you require a callback to be defined
and non-null (such as error handlers, progress handlers, predicate filters,
etc). An event is just a standard delegate with an internal wrapper class
around it, primarly needed to allow only the owner to invoke it.

--
William Stacey [MVP]

| I've used delegates fairly heavily for several years in C# for event
| handling and for starting threads.
|
| Does anyone have any real-world scenarios where delegates were both
| extremely useful and extremely appropriate (as opposed to other
programmatic
| means or mechanisms) that were NOT related to events and ThreadStarts?
|
| Thanks,
| Jon
|
|
 
Sure. See the System.Text.RegularExpressions.Regex.Replace method
(http://msdn2.microsoft.com/en-us/library/cft8645c.aspx). It takes a
MatchEvaluator delegate as a parameter, allowing you to define whatever
method you wish for replacing matches in a regular expression.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

What You Seek Is What You Get.
 
Jon said:
I've used delegates fairly heavily for several years in C# for event
handling and for starting threads.

Does anyone have any real-world scenarios where delegates were both
extremely useful and extremely appropriate (as opposed to other programmatic
means or mechanisms) that were NOT related to events and ThreadStarts?

Delegates, in .NET, are a route to first-class functional programming.
Check out ML, Haskell or Lisp for more on functional programming.
Anonymous delegates with closure greatly multiply the usefulness of
delegates with respect to programming in a functional style in C# etc.

For example, I've used delegates as combiners in fold-like scenarios,
projectors in map-like scenarios (think List<T>.Convert), predicates in
filter-like scenarios (think List<T>.FindAll), etc.

It's all basically about parameterising one function with one or more
other functions, and using functions to compose new functions, rather
than just using non-function values.

For example, consider a simple predicate constructor:

Predicate<T> Not(Predicate<T> f)
{
return delegate(T value) { return !f(value); };
}

Using various functions like this, one can compose functions out of
other functions, and in turn use them as arguments to other functions.

-- Barry
 
Michael C said:
I have an imaging app that can capture images from multiple sources, I use a
delegate so that whatever code is capturing the image can send new images,

eg
CaptureFromScanner(new CallbackDelegate(this.ImageCaptured));
one of the methods CaptureFromClipboard doesn't have a form or any object
associated with it so can't raise events.

You could have a static event...

Again these function could be in static methods so can't do events.

Could you explain this a bit more? There's nothing to stop you from
using static methods with events. Both of your examples sound like they
could fairly easily be events, to be honest. Not that they have to be,
of course...
 
Jon Skeet said:
You could have a static event...



Could you explain this a bit more? There's nothing to stop you from
using static methods with events. Both of your examples sound like they
could fairly easily be events, to be honest. Not that they have to be,
of course...

hmmm, I never thought about static events. Thanks for the tip, you learn
something new every day. However, a delegate is pretty suited to this
situation. I have one class that has a DoCapture static method with an enum
to specify the source (scanner, camera, capture card etc). DoCapture has a
switch statement that will trigger each of the capture methods and pass on
the delegate. If I had events it would be more complicated I guess.

Michael
 
Back
Top