ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button
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...


1 to 1 of 1
1 to 1 of 1