By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,911 Members | 1,180 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,911 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Sep 17 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"Jon Davis" <jo*@REMOVE.ME.PLEASE.jondavis.netwrote in message
news:Ot**************@TK2MSFTNGP06.phx.gbl...
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
Sep 17 '06 #2

P: n/a
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]

"Jon Davis" <jo*@REMOVE.ME.PLEASE.jondavis.netwrote in message
news:Ot**************@TK2MSFTNGP06.phx.gbl...
| 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
|
|
Sep 17 '06 #3

P: n/a
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 Davis" <jo*@REMOVE.ME.PLEASE.jondavis.netwrote in message
news:Ot**************@TK2MSFTNGP06.phx.gbl...
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


Sep 17 '06 #4

P: n/a
Jon Davis wrote:
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<TNot(Predicate<Tf)
{
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

--
http://barrkel.blogspot.com/
Sep 17 '06 #5

P: n/a
Michael C <no****@nospam.comwrote:
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...

<snip>
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 18 '06 #6

P: n/a
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
Michael C <no****@nospam.comwrote:
>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...

<snip>
>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...
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
Sep 18 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.