On Oct 23, 3:40*pm, samadams_2...@yahoo.ca wrote:
Hello,
I'm trying to figure out why delegates are such a great thing. *For
example, I came across the following line of code:
"This ability to refer to a method as a parameter makes delegates
ideal for defining callback methods. For example, a sort algorithm
could be passed a reference to the method that compares two objects.
Separating the comparison code allows the algorithm to be written in a
more general way."
Can someone explain this to me?
Thanks
Sam
I was like you about three months ago.
My newbie advice: get the book by Albahari C#3.0 in a nutshell. or,
just study delegates until your eyeballs drop out. Then study events
(a form of delegate). After about the tenth example it finally dawns
on you.
The way I like to think of it (and others on this board disagree):
delegates are a kind of global "GOTO" statement, where a function/
method can fire a delegate GOTO and have several portions of code that
are "registered" to listen to the delegate execute code the minute the
function/method fires, even when the function/method is unaware of
these registered portions of code. So you can have multiple "GOTO"s
execute simultaneously.
If that sounds complicated, it is. Believe me, delegates will make
your head hurt. But there's no other way to learn them. Nothing
anybody says in this thread--no matter how right--even me--will make
you understand delegates. You just have to keep trying until you
finally 'get it'.
Good luck.
RL