Hacker News new | ask | show | jobs
by iGoog 2647 days ago
I gotta ask what in particular "problems" C# solves over Java, and I'm not talking about J2SE 1.4. Having dipped into C#, it feels like the designers resented OOP and reusable code. C# is not the new kid on the block and is almost 20 years old now.
2 comments

Over that 20 years, C# has been continuously improved with language features added at a faster rate than Java. Some examples: * properties * LINQ * async/await * dynamic types * better generics * inferred typing - `var` * null conditional operators * custom iterators - `yield` * pattern matching

Also, .net is more of a “pit of success” language — Java with all its legacy baggage requires a lot more knowledge to avoid mistakes.

so... properties that require a steeper learning curve, a system that's worse far worse than JPA+Spring Data (or you could use the hibernate in both languages - a java development), another word for var/let, I'd disagree about generics, var/let again, I wonder what rust devs would say about these null inline conditionals that require more knowledge, and... yield is actually neat if also not another non-traditional baggage thing to learn. Anyhow, I guess my take is that Rust is a better language than C# in all the ways that C# might be better than Java and more... Except that C# is a lot like Java and is the language of choice if you use Unity - so it does have a big awesome ugly library that Java does not have. Really we should feel sorry about all the Unity baggage C# devs get to deal with. Then there's Microsoft land, which has not inspired me of code quality and can't get out of VB think.
Hibernate is an ORM. LINQ is just what the name says it is language integrated query. Linq is understood by the compiler, translated into expression trees at run time and then any third party provider can translate it to the destination language. You can pass Hibernate expressions around and depending on context, they can get translated to regular IL when working with Lists, Sql when working with RDMSs or MongoQuery when working with Mongo, etc.
> it feels like the designers resented OOP and reusable code

Can you give some examples, that wouldn't be equally applicable to Java?

Maybe it's less of a C# thing, and more of a Unity thing - but I'd say that Unity has some major architectural anti-patterns - which may just be to appease the ease of a the awesomeness of a wysiwyg type game engine.
Polymorphism and inheritance by default?
You mean the fact that methods aren't virtual by default?

As Hejlsberg explained, the problem with virtual-by-default is that every time one of your public methods calls another public method on the same object, it makes it that much more complicated to reason about your invariants. If the called method is non-virtual, then you're in full control, and you know exactly what happens. But if it can be overridden, then you need to define a contract for overriders to follow, and you can't assume that it does anything other than what's expressed in that contract (in particular, you can no longer call that method without ensuring that all class invariants hold, even if it's expensive). Furthermore, the overriders often also need to know which other methods on the class do or do not call this method.

In practice, this is so much hassle that not even the Java standard library does that. For example, given ArrayList, if you override add(), does that affect addAll()? The docs don't say. So in practice, the only things that are safe to override is what the docs say are safe to override... which is to say, exactly like "virtual", except it's not enforced by compiler. Overriding random methods often works, but is a hack not dissimilar to using Reflection to access non-public members - even if it works, you're relying on implementation details of the class.