Women in Technology

Hear us Roar



Article:
  A Java Programmer Looks at C# Delegates
Subject:   anonymous inner classes
Date:   2003-05-22 09:27:29
From:   anonymous2
I've been working exclusively with C# (at work, at least) since January 2003, and before that was a Java programmer for a couple of years. Personally I find delegates to be only a slight improvement over anonymous inner classes. While the anonymous class route does require you to put in a couple of braces (which some consider "ugly" or "unreadable"), I personally find it quite natural. IntelliJ will even stub out all of the methods for you automatically, so you don't actually end up doing much more typing than with a delegate.


And one advantage of the anonymous class approach is that you end up passing a real object, one that can have more than one method and keep its own state--as opposed to a delegate where really you are just passing a pointer to a single method (though in all fairness that is by far the most common situation).


As a side note, one disadvantage of your solution is that the compiler doesn't help you make sure the delegated methods conform to the delegate signature.


I'm really looking forward to anonymous delegates in C#. That will be the most useful solution overall, IMHO...

Full Threads Newest First

Showing messages 1 through 2 of 2.

  • anonymous inner classes
    2003-05-22 11:30:52  anonymous2 [View]

    Snonymous inner classes in Java play two roles, one is standing in for a reference to a method, to which delegates do represent an improvement, at least in terms of avoiding syntatic clutter.

    The second semantic feature they provide is that of a closure (albeit with a few limitations), or in other words, a block of code which captures state from its lexical context. In this role they are very useful, although the wordiness of the construct in Java leads me to use them less than I would in languages with more spartan closure-like constructs.

    So delegates should not be seen as a complete replacement for anonymous inner classes, but a specialization that helps reduce syntactical clutter for that specific use.

    And you are correct that in our solution, attempting to create a delegate on a method that does not provide the correct type signature will not be discovered till runtime. I'm not sure how you would avoid that problem short of extending the compiler itself.
    • anonymous inner classes
      2003-05-23 12:08:36  anonymous2 [View]

      Delegates actually are bound to a class (although delegates referencing static methods are also possible).
      For example, you could have "delegate int CountDelegate()" and then have "new CountDelegate(CounterInstance.CountMethod)". In that case the CounterInstance holds the counter state between delegates calls. This is similar to what an inner class would achieve.

      I detail this some more in an article (http://blog.monstuff.com/archives/000037.html) that shows that the delegate holds both an object (state) and a method pointer, and also goes through the implementation details.

      Cheers
      Dumky