5 ms·
Anonymous inner classes are more powerful than c# anonymous types. I think c# wins out overall, but there are a few corners where Java has features c# is missi
by Pxtl 9y ago
Anonymous inner classes are more powerful than c# anonymous types. I think c# wins out overall, but there are a few corners where Java has features c# is missing, coming from the fact that c# was a bit more pragmatic where Java was a bit more oop-purist in its origins.
- noblethrasher 9y agoAnonymous delegates (which have been available since 2.0) are the equivalent feature in C#, at least as far as use cases go.
- Pxtl 9y agoWhile anonymous inner classes are more verbose, they're also more powerful because you can create a whole class instead of a single method.
- noblethrasher 9y agoBut, 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.
- pjmlp 9y agoDuring 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.
- jayd16 9y agoDelegates 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.
- noblethrasher 9y ago> 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-expression-exist-in-c-sharp-like-in-f/20874683#20874683 https://stackoverflow.com/questions/20703002/does-object-exp...
- tigershark 9y agoWhat are the features that c# is missing? As the other comment explained delegates are the equivalent of anonymous inner classes and are far better. Checked exceptions are really a nuisance, I would call them a negative feature. Java enums in c# are basically classes with readonly properties if you need to pass constructor arguments. Streams in Java are a huge clusterfuck. I can't think to anything that Java has that is better than c#.
- 35bge57dtjku 9y agoIt'd be great if the secret to language design was cramming in as many features as possible.
- int_19h 9y agoDelegates are not the equivalent of anonymous inner classes. They're commonly used for similar design patterns (observer and other callbacks), yes, but they're not the same thing. To be more specific, an anonymous delegate / lambda can be thought of as an implementation of an interface with a single method. But with Java anonymous inner classes, you can implement multiple methods at once. The reasons why Java enums are better, is because they're actually constrained to the domain you specify. In C#, the range of values for any enum is the same as its underlying integer type - it's just that some of those values have names, while others don't. But it's always legal to cast (and few people know this, but 0 is always a valid enum value that you don't need to cast) - so any method you write that accepts an enum value has to validate it. In Java, since enum is basically a final class with a bunch of private singletons, you are guaranteed that the reference you get points to one of those. That said, given that C# now has pattern matching, and it seems to be getting more powerful with every release, I'd be surprised if it didn't get something like case classes very soon.
- noblethrasher 9y agoDelegates and anonymous inner classes are equivalent, modulo API: //Java Bax.addHandler(new Foo() { @Override public void Bar() { doSomething(); } @Override public int Baz(X n) { return computeInt(n); } }); //C# Bax.AddHandler(() => DoSomething(), n => ComputeInt(n)); But, anonymous delegates and lambdas are at once syntactically nicer and more powerful, because of type inference and the fact that they can capture local variables. Regarding Java enums, the actual equivalent feature in C# is class nesting. There is a tiny bit more ceremony involved in defining them, but they're more flexible than Java enums. For instance, you can decide whether or not to implement them as readonly (final) fields, or as static properties, depending on how much you care about cache friendliness. public abstract partial class SomeEnum { private SomeEnum(){} public abstract int Biz(); sealed class AFoo : SomeEnum { public override Biz() => 2 } sealed class ABar : SomeEnum { public override Biz() => 4 } sealed class ABaz : SomeEnum { public override Biz() => 8 } } //Singleton implementation: public abstract partial class SomeEnum { public static readonly SomeEnum Foo = new AFoo(); public static readonly SomeEnum Bar = new ABar(); public static readonly SomeEnum Baz = new ABaz(); } //Cache friendly implementation: public abstract partial class SomeEnum { public SomeEnum Foo => new AFoo(); public SomeEnum Bar => new ABar(); public SomeEnum Baz => new ABaz(); } It's also worth noting that extension methods on C# enums give you most (all?) of the power of Java enums.