On 7/14/2011 11:28 PM, Peter Duniho wrote:
> However, note that the majority of the time in Java, the class being
> implemented anonymously implements just a single member, usually of an
> interface. As such, anonymous methods in C# do the job just fine, where
> a delegate type takes the place of the single-member interface that was
> used in Java (see Arne's examples).
>
> For anything more complicated, I'd argue that an anonymous type in Java
> probably was not the best choice in the first place. Methods that have
> within them embedded code can be hard to follow anyway, but this gets
> even worse when there are multiple methods.
It is common in Java especially in Swing to do it that way.
> It's one of the reasons that the lambda syntax for anonymous methods in
> C# can be so nice, as it takes what could be a lot of clutter and tidies
> it up a bit.
>
> As useful as anonymous methods can be, with features like variable
> capturing (a nice improvement over the Java requirement that referenced
> variables must be "final"), in both Java and C# if the anonymous
> class/method is starting to get too large, it's probably time to start
> thinking about moving all that code over to a dedicated class.
The Java final requirements mean that there are never any doubt about
how the variable is used.
It happens in C# that captured variables has a different value than
what the developer thought.
Arne
|