motivation behind delegates

  • Thread starter Thread starter Flip
  • Start date Start date
F

Flip

I'm sorry for the newbie question, but I'm a bit confused. I'm reading
about Delegats in Jesse Liberty's C# Programming book and, well, I'm
confused as to the motivation behind delegates. On the surface, they look
exactly like using interfaces in java, so why have another construct to
confuse things?

I know they work "like function pointers" but the whole reason I went to
java was to avoid pointers! And before java, I used VB, so pointers are not
something advantageous to me. And I understand delegates are how .net does
event driven coding, but why can't interfaces be used to accomplish the
samething?

Thanks for any advice.
 
Flip said:
I'm sorry for the newbie question, but I'm a bit confused. I'm reading
about Delegats in Jesse Liberty's C# Programming book and, well, I'm
confused as to the motivation behind delegates. On the surface, they look
exactly like using interfaces in java, so why have another construct to
confuse things?

They make things a lot easier than interfaces:

1) You don't need to declare that a type implements them
2) You can make your delegate private
3) You can have the same delegate signature implemented several times
in the same type
4) Multicast delegates act as a very convenient way to implement event
handlers
I know they work "like function pointers" but the whole reason I went to
java was to avoid pointers! And before java, I used VB, so pointers are not
something advantageous to me. And I understand delegates are how .net does
event driven coding, but why can't interfaces be used to accomplish the
samething?

They can for the most part - they're just not as convenient.
 
Back
Top