When to use Delegates

G

Guest

I have a logger component that logs to multiple sources, ie textfile,
eventlog etc.
and I have two methods that depending on where I call up my logger comp. one
of them will be called. For ex. if I throw an exception I want to call one
method and if I dont, I am just logging some info to eventlog, I will call
the other. Now i'm wondering would it make sense to use delegates, one for
each method to call methods in my window service? How would I go about coding
it? Or should I not even bother and just instantiate the logger class and
call the methods when needed?
 
P

Peter Duniho

I have a logger component that logs to multiple sources, ie textfile,
eventlog etc.
and I have two methods that depending on where I call up my logger comp.
one
of them will be called. For ex. if I throw an exception I want to call
one
method and if I dont, I am just logging some info to eventlog, I will
call
the other. Now i'm wondering would it make sense to use delegates, one
for
each method to call methods in my window service? How would I go about
coding
it? Or should I not even bother and just instantiate the logger class and
call the methods when needed?

From your description, I don't see anything that clearly indicates a need
for delegates. It depends on how you would use the delegate. If you are
envisioning a design in which one of two delegates is chosen from
depending on the situation (exception or event logging), then it seems to
me that you might as well just call the method you need to be called. On
the other hand, if you are envisioning a design in which you have a single
delegate, initialized to one of two values depending on the situation,
then perhaps a delegate does make sense.

It just depends, and I don't see enough information in your post to be
able to determine what you intend.

More generally...

If you can accomplish the same exact behavior without using delegates,
then why would you use delegates?

Conversely, if you cannot accomplish the same exact behavior without using
delegates, then why would you attempt to do so?

To me, these are interesting questions to ask yourself when you are
thinking about the design.

It seems to me that delegates are useful in a particular type of
situation: you want to be able to specify at run-time a method to call,
but don't have a situation that lends itself to creating an abstract class
that clients can use to provide derived versions of at run-time. If the
code doing the calling will always call the same method, and that method
can be included at compile time, then there's no need for either.

As an example of using an abstract class, many of the logging type
features in .NET use something that derives from TextReader, usually
taking a StringReader or StreamReader instance as appropriate. No need
for delegates there: each derived class has specific functionality that is
mapped to the general-purpose methods specified in TextReader. By
implementing the derived class (or in this case, using a built-in
implemented derived class) and providing that as the base class, any code
that needs to call different methods depending on context can do so
without know specifically what code it needs to call.

Events are .NET's classic example of the use of delegates (though by no
means the only example). At run-time you have a situation in which one
class wants to be callable by another class, but it doesn't make sense to
derive that one class from some specific abstract class.

You could accomplish similar effects by using an interface, but if you've
got a class that will have dozens of different event handlers, all of
which are subscribed to different kinds of events, the list of interfaces
would get out of hand, and it would be sort of silly to have dozens of
interfaces, all of which have just one method in them. Here, a delegate
works nicely, as the method itself can be implemented in any class you
like, without worrying about inheritance structures, and any class can
implement as many delegates as needed without creating a lengthy,
confusing inheritance list (acknowledging of course that in C#, there's
really only one class that's inherited at most...interfaces are only sort
of like inheriting a purely abstract class).

So, that's the general description. You can either look at that and try
to apply it to your own situation, or you can clarify what your own
situation actually is, so that others can provide better insight as to
whether a delegate seems appropriate or not.

Pete
 
J

Jon Skeet [C# MVP]

JJ said:
I have a logger component that logs to multiple sources, ie textfile,
eventlog etc.
and I have two methods that depending on where I call up my logger comp. one
of them will be called. For ex. if I throw an exception I want to call one
method and if I dont, I am just logging some info to eventlog, I will call
the other. Now i'm wondering would it make sense to use delegates, one for
each method to call methods in my window service? How would I go about coding
it? Or should I not even bother and just instantiate the logger class and
call the methods when needed?

I'd go with the non-delegate approach to start with - I can't
immediately see where the delegates would make life easier. (I'm not
saying they're not useful in general, just that I can't see the benefit
here.)
 
B

Bob Johnson

Hi Jon,

What *would* be a scenario or general guidelines where delegates would make
life easier. Just looking for broad guidance (no particualar scenario in
mind).

Thanks.
 
P

Peter Duniho

What *would* be a scenario or general guidelines where delegates would
make
life easier. Just looking for broad guidance (no particualar scenario in
mind).

I described that briefly in my post, in reply to your original question.
Was there something about that example that didn't make sense to you, or
didn't seem applicable?

Generally speaking, delegates are very much like function pointers in C.
But because of the "event" type in C#, they are much more integral to the
use of the language. The .NET Framework uses events extensively to allow
one class to extend another class without having to derive from it, or to
provide inter-class communication, and without delegates events just don't
work. Delegates are also used in a variety of callback scenarios:
predicates used for searching collections, methods intended as entry
points for threads, callback routines for asynchronous i/o, etc.

Pretty much any time you have a situation in which you want a method to be
called by some code that does not know in advance which method will be
called, and in which there's no natural overridable class that can provide
a virtual method to do that, that's the sort of situation you'd use a
delegate in.

Pete
 
B

Bob Johnson

RE:
<< I described that briefly in my post, in reply to your original question
I'm not the person who posted the original question in this thread. Thanks
for offering further clarification anyway.
 
P

Peter Duniho

I'm not the person who posted the original question in this thread.
Thanks
for offering further clarification anyway.

Ah...my mistake. Sorry for any confusion.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top