Re: Don't understand delegates

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



On Fri, 25 Apr 2008 08:09:18 -0700, Cowboy (Gregory A. Beamer) <NoSpamMgbworld@xxxxxxxxxxxxxxxxxx> wrote:

Ignacio points out the primary reason: You are not sure what you are calling
until runtime.

Careful with the "until runtime". _Some_ code usually knows what's going to be called at compile time. It's just that the _calling_ code doesn't know until runtime.

With that in mind, I'd say that technically speaking, all uses of delegates fall into this category. For example...

Delegates are aslo useful for event handlers, when you only want a specific
bit of code called in a certain instance.

But the reason they are used for event handlers is that at compile time, the implementer of the event doesn't know what code will be called. The ..NET Forms classes are a classic example of this. Microsoft can't possibly know what your own code would be when they provide an event. So using a delegate allows other code to provide the reference to the method to be called, well after the point at which Microsoft's code was compiled.

Another reason to delegate is callbacks, which cannot be coded directly as
method calls.

That's not strictly speaking correct, depending on what you mean by "as method calls". Java doesn't have any idea like delegates, and yet it can implement the same sort of behavior. It uses interfaces instead. And those are coded directly as method calls (i.e. a method defined in an interface implemented by whichever class wants to provide the callback). This could be done in .NET, and in fact for more elaborate APIs is in fact used quite a lot.

But regardless, that's still a subset of the general "you don't know at compile time the exact method that will be called".

I prefer delegates, and I feel that for single-method situations they are way more convenient than having to create a whole interface and then implement it in each class that wants to provide a method to call at specific times (whether to support events, i/o callbacks, whatever). But you don't _have_ to have delegates to allow for callbacks that aren't known at compile time.

Pete
.



Relevant Pages

  • Re: alowing delegates in attribute ctors
    ... is it too much of a stretch to have a ... Keep in mind that delegates can refer to instance methods too. ... no way at compile time to know which instance the method should be ...
    (microsoft.public.dotnet.framework)
  • Re: why doesnt this work? final assignments and switches
    ... switch cases want [values known at compile time. ... But if you are calling them A0, ... Coaching, problem solving, economical contract programming. ...
    (comp.lang.java.programmer)
  • Re: int to double in asm (just curious...)
    ... This conversion is done on compile time. ... > I was wandering if there's any difference at all in calling for ...
    (microsoft.public.vc.language)
  • va_arg()
    ... I have a question about va_argusage: ... Is there any way how I can check or ensure at compile time that ... I understood that calling va_argwhen there are no next arguments ... causes an undefined behaviour. ...
    (comp.lang.c)