| 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.
|
||
Showing messages 1 through 1 of 1.
-
anonymous inner classes
2003-05-22 11:30:52 anonymous2 [View]



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.