Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Anonymous delegates (which have been available since 2.0) are the equivalent feature in C#, at least as far as use cases go.


Delegates are nice in the common case but I do miss anonymous classes.

Anonymous classes have better oo organization than passing in two or more delegates. Sometimes variable capture doesn't cut it or isn't as clear.

..And the biggest thing. I can pass it in to a method that already takes a class and not a delegate.


> And the biggest thing. I can pass it in to a method that already takes a class and not a delegate.

One solution is the build a small inventory of "ad-hoc" classes that just wrap a delegate (usually a closure), as described here: https://stackoverflow.com/questions/20703002/does-object-exp...


While anonymous inner classes are more verbose, they're also more powerful because you can create a whole class instead of a single method.


But, they're equivalently powerful with respect to an API.

In Java, a method might expect one instance of a class, to which you can pass an instance of anonymous class, whereas in C# a method might expect a set of delegates, to which you can pass a set of anonymous delegates or lambda expressions.

However, thanks to C#'s type inference, it actually ends up being far less verbose, yet more flexible, because anonymous delegates and lambdas can close over local variables. So, strictly speaking, they're more powerful than anonymous inner classes.

On the other hand, as someone that greatly admires Smalltalk, I sometimes envy Java's slightly better purity.


During Java early days, I was a big fan of Eclipse's Java browsing perspective, it was a way to feel like I was still using Smalltalk.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: