Fun with delegates

This last month, after over three years of working with .net, I finally understood what a delegate is. Now understand that I could correctly use the syntax and setup event handlers and other common uses of delegates before then. Still, I just didn’t get it. Part of it is that the grammer used with event handler examples


3 responses to “Fun with delegates

  1. Jimmy Nilsson

    Hi Philip,

    I can’t wait until you write more about this! VERY interesting!

    Best Regards,



  2. I have to say that I read this and thought: Yep, Delegates are cool and then moved on 😉

    Then Jimmy mentioned your post in an email and we got to talk. The point that you can define the behavior of an object by inheriting from Delegates instead of through the implemenattion of interfaces

    (public class Gun : Weapon, Fireable) is indeed a very poweful concept!

    It basically allows us “weave in” the behaviour of an object at run-time by specifying the target method of the Fireable (AOP like). One problem with this approach compared to interfaces though. We don’t have multiple inheritance so we can only inherit from one Delegate class 😦

    Maybe the Delegates should become encapsulated in interfaces like this:

    public class Gun : IFireable, ICleanable

    public interface IFireable {Fire();)

    public interface ICleanable {Clean();)

    The constructor of the Gun could then be supplied delegates for the Fire() & Clean() methods. These delagtes could point to “whatever-runtime-loaded-code” we wish and we would thus be able to supply the object with behaviour at runtime. I’m not sure if this is cool or dangerous…but it is at least interesting 😉


  3. Philip Nelson

    I haven’t had time to really explore this, except in my head while driving and the like. Yes, inheriting from Delegate would be a problem, actually, I’m not sure what it would give you. Interesting in it’s own right. But I was thinking more on the lines of burying the delegate in a generated implementation class. Whether done by a framework at runtime so you would use


    or at compile time with a custom build action is still a big question. I would like to prototype some different ideas and see what feels best.