Just trying to get my head around when to use delegates and when not to.
I understand delegates and use them with collection sorts and finds.
But in an example program I was playing with I am a little confused as to
why I would use delegates:
*****************************************************************
class Program
{
public delegate void MessagePrintDelegate(string msg);
protected delegate void LongRunningDelegate(MessagePrintDelegate
mCallBack);
static void Main(string[] args)
{
PrintMessage("This is a direct call to PrintMessage");
MessagePrintDelegate mpDel = new
MessagePrintDelegate(PrintMessage);
LongRunningDelegate lrd = new
LongRunningDelegate(LongRunningMethod);
mpDel("This is the direct Call to delegate");
lrd(mpDel);
LongRunningMethod(mpDel);
Console.Read();
}
static void LongRunningMethod(MessagePrintDelegate mpd)
{
for (int i = 0; i < 99; i++)
{
if (i % 10 == 0)
{
mpd(string.Format("Making progress. {0}% Complete.",
i));
}
}
}
static void PrintMessage(string msg)
{
Console.WriteLine("[{0}] {1}", DateTime.Now.ToShortTimeString(),
msg);
}
}
*****************************************************************
In the above code, doing the line:
mpDel("This is the direct Call to delegate");
wouldn't make any sense as I could just use a direct call to
PrintMessage() - which is all the delegate is doing.
In the line:
lrd(mpDel);
I am using a delegate to call a method to call a delegate that calls
PrintMessage.
This makes little sense as you can just call,
LongRunningMethod(mpDel);
which is a direct call to the method to call a delegate which call
PrintMessage.
In both cases, I using a delegate call when I don't need to.
What I am trying figure out is when I would need to use a delegate and when
I would not.
Thanks,
Tom
<snip>
> In the above code, doing the line:
>
> mpDel("This is the direct Call to delegate");
>
> wouldn't make any sense as I could just use a direct call to
> PrintMessage() - which is all the delegate is doing.
You could do - if you knew that PrintMessage is all you ever want to
do. It doesn't allow other people to determine how the logging should
occur though.
> In the line:
>
[quoted text clipped - 9 lines]
> which is a direct call to the method to call a delegate which call
> PrintMessage.
In that case there seems no point at all, indeed.
> In both cases, I using a delegate call when I don't need to.
>
> What I am trying figure out is when I would need to use a delegate and when
> I would not.
Use it when you want to be able to specify behaviour in a flexible way,
without using inheritance.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
tshad - 02 Mar 2008 17:27 GMT
> <snip>
>
[quoted text clipped - 33 lines]
> Use it when you want to be able to specify behaviour in a flexible way,
> without using inheritance.
By flexible you mean what?
Also, if I make a change where I add another delegate this delegate requires
2 objects:
**************************************************************
class Program
{
public delegate void MessagePrintDelegate(string msg);
protected delegate void LongRunningDelegate(MessagePrintDelegate
mCallBack);
private delegate int GetCountDelegate(Person obj1, Person obj2);
static void Main(string[] args)
{
PrintMessage("This is a direct call to PrintMessage");
MessagePrintDelegate mpDel = new
MessagePrintDelegate(PrintMessage);
LongRunningDelegate lrd = new
LongRunningDelegate(LongRunningMethod);
mpDel("This is the direct Call to delegate");
lrd(mpDel);
LongRunningMethod(mpDel);
GetCountDelegate gcd = new GetCountDelegate(GetCount);
int count = gcd(new Person(), new Person());
int count1 = GetCount(new Person(), new Person());
Console.WriteLine("Count received: {0} and {1}", count, count1);
Console.Read();
}
static void LongRunningMethod(MessagePrintDelegate mpd)
{
for (int i = 0; i < 99; i++)
{
if (i % 10 == 0)
{
mpd(string.Format("Making progress. {0}% Complete.",
i));
}
}
}
static void PrintMessage(string msg)
{
Console.WriteLine("[{0}] {1}", DateTime.Now.ToShortTimeString(),
msg);
}
static int GetCount(object obj1, object obj2)
{
Random rnd = new Random();
return rnd.Next();
}
class Person
{
}
class Contact : Person
{
}
}
***************************************************************
and I have to add the parameters - which makes sense but in my other
delegate call, I didn't even though it required a parameter:
lrd(mpDel)
lrd is calling mpDel which is a delegate which calls a method PrintMessage
which requires a string parameter. But:
int count = gcd(new Person(), new Person()));
here gcd is calling a method that has 2 parameters and has to pass the
parameters.
lrd is callint the mdel which is a PrintMessage method that requires a
string - but I can't see here the string is actually set - but it works.
Why is that?
Thanks,
Tom
Jon Skeet [C# MVP] - 02 Mar 2008 18:51 GMT
> > Use it when you want to be able to specify behaviour in a flexible way,
> > without using inheritance.
>
> By flexible you mean what?
I mean when you want to be able to specify different behaviour. For
example, you wouldn't want to have to derive a new subclass of Button
every time you wanted to react to a "click" in a different way.
Delegates allow you to specify what should happen when the button is
clicked, in a flexible way.
> Also, if I make a change where I add another delegate this delegate requires
> 2 objects:
<snip>
> lrd is callint the mdel which is a PrintMessage method that requires a
> string - but I can't see here the string is actually set - but it works.
>
> Why is that?
lrd is calling LongRunningMethod, and LongRunningMethod calls the
passed-in delegate with a string in this line:
mpd(string.Format("Making progress. {0}% Complete.", i));
It's only the *invocation* of the delegate which makes you provide the
arguments.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk