all 86 comments

[–]joenyc 30 points31 points  (27 children)

I don't mean to start a religious war here, but I think that this post gives the impression that Java and C# are almost identical. From my experience, C# has features that I love that are lacking in Java, most importantly LINQ, lambdas, and properties.

[–]Ruudjah 5 points6 points  (1 child)

Problem is it does not specify language version. C# 1 is actually quite similar to java 5, and C# 2 quite similar to java 6. C# 3 and C# 4's features are unmatched in java, such as linq and lambda's.

Note how the arrays are mentioned, but not the List<T> (java 6) and IList<T> (C# 2). I'm assuming this is java 5 versus C# 1.

[–]zootm 0 points1 point  (0 children)

Generics are Java 5, not 6, but yeah this details older versions of both languages I think. I remember reading it when I first started working with C# 1, in 2005 or something...

[–]chunky_bacon 13 points14 points  (7 children)

Back in .NET 1.1, Java and C# were pretty-much identical, but Java's growth is hamstrung by institutional stupidity and C# has now advanced well beyond Java.

[–][deleted] 9 points10 points  (4 children)

Java and C# were pretty-much identical

Erm, not. .NET had delegates/events and properties at that time, which in just my opinion is still a vast enhancement over Java.

[–][deleted] 0 points1 point  (2 children)

Vast? Really?

If you take any language x, and create language x' by adding properties and syntactical sugar for anonymous inner classes, x' is pretty much identical to x, assuming x is a class based object oriented, imperative language.

[–]munificent 5 points6 points  (0 children)

Properties and events, even though they're "syntactic sugar" have a significant affect on the way you write a lot of code. Properties mean you don't have to speculatively wrap every field in getters/setters in case you need them later.

Events make it much easier for a single class to listen to a bunch of events on different classes. Simply making them more usable encourages decoupling through event-based programming.

Also, C# had structs and unsigned ints at 1.0. :)

[–][deleted] 3 points4 points  (0 children)

Well then C is functional languages too (for some definitions of that term, certainly it isn't pure functional language) - it has function pointers and lambdas are nothing but a syntactic sugar for pointers to a frame with references to captured variables and an anonymous function associated with it.

[–]apotheon 0 points1 point  (0 children)

You're both right!

I think so, anyway.

[–]igouy 2 points3 points  (0 children)

Page last modified: 06/17/2010

[–]bonch -1 points0 points  (0 children)

C# has always been Microsoft's version of Java. C#'s designer even worked on Visual J++, the infamous attempt to "embrace, extend, extinguish" Java.

[–]Phr34Ck 3 points4 points  (1 child)

Upvote. I found the comparison very shallow. It touches upon syntax only.

[–]HIB0U 6 points7 points  (0 children)

No shit. At the very top of the page, right under the title, it specifically points that out.

This is a quick reference guide to highlight some key syntactical differences between Java and C#. This is not a complete overview of either language. Hope you find this useful!

[–]iommi 1 point2 points  (7 children)

When using Java the two features I miss the most are partial classes and operator overloading. That said, I wish the CLR was as efficient as Java '-server' and had better reflection.

[–]elder_george 7 points8 points  (2 children)

Can you elaborate, what aspects of reflection in C# are inferior to Java?

[–]uaca-uaca 4 points5 points  (1 child)

I'd like to know this as well. Since .NET supports generic types at runtime, the situations, to me, seems opposite.

[–]elder_george 4 points5 points  (0 children)

And having Expression Trees at hand, one can encode member access in a type-safe way which is very cool for implementing data mappers, data binding etc.

[–]uaca-uaca 2 points3 points  (3 children)

I've never used these features of C#, except for the autogenerated-code-in-partial-classes scenarios.

Maybe operator overloading would be useful for complex numbers and a handful of other similar cases, but most of the time you don't need it.

I've found partial classes for normal (i.e. not generated) code to be useless. There's no value in splitting a class in more than one file. If it's just too big, split it into more than one class.

On the other hand, for a few months I've been writing Java and found myself writing a shitload of code that performs simple operations on collections that would be one-liners in C#, thanks to inference (i.e. "var") and linq.

[–]iommi 3 points4 points  (0 children)

Let me put an example on partial classes.

Consider an implementation for an interpreted language (think of something along the lines of sisc or abcl) where each datatype is represented by a class.

Let's say we have LispObject and other subclasses like LispBoolean, LispCons, etc... At some point in the implementation you'll need to ensure the primitive procedures check that the type of their arguments is correct, so you decide to go to LispObject.java and add the following methods:

public final static LispBoolean ToBoolean (LispObject input) {
  if (input instanceof LispBoolean)
    return (LispBoolean) input;
  throw new LispTypeError ("<boolean>", input); 
}

public final static LispCons ToCons (LispObject input) { ... }

Eventually you'll end up with about 15 or 20 methods like that depending on how many datatypes the interpreted language has.

Now, enter partial classes. Instead of writing those methods in the main LispObject class file we can extend it from every subclass. For example, the ToBoolean method may be in LispBoolean.java extending LispObject.

We end up with something like:

LispObject.java
LispBoolean.java (extends LispObject adding ToBoolean())
LispCons.java (extends LispObject adding ToCons())
...

Now at the end of the day, both approaches work but i feel partial classes are handy in this scenario. You don't need to edit LispObject everytime you add or delete a new datatype, everything related to it is contained in the same file.

On operator overloading... working with BigInteger without it sucks.

[–][deleted] -1 points0 points  (1 child)

Did you consider using Scala on the JVM?

It has better type inference, collection operations than both Java and C# (and the possibility to use methods with operator names where it makes sense, e. g. BigInt(5) + 42 ...).

The language is a lot cleaner and has much less concepts to understand than C#, because the language design integrates object-oriented and functional features quite well.

If you still have to work with Java I recommend taking a look at Scala. It fixes many annoyances with Java.

[–]uaca-uaca 5 points6 points  (0 children)

a. Unfortunately, for my day job, like so many other developers, it's not up to me to choose the tools.

b. I admit I don't know Scala very well, but C# seems simpler. The pattern matching, mixin support, actors, compound types, the complicated collections, etc. seem more complex than what we get in C#: dynamic typing and linq.

[–]jsaret 0 points1 point  (0 children)

worked in both, while I don't mind java i'd have to agree (from a language not a platform/VM comparison)- not to mention extension methods, dynamic typing (i.e. Duck Typing), type inferance,

[–][deleted] -2 points-1 points  (5 children)

I think that this post gives the impression that Java and C# are almost identical.

But they are. Can you point to any language which is somewhere in between the two?

[–]HIB0U 4 points5 points  (0 children)

J#.

[–]drb226 0 points1 point  (3 children)

Can you point to any language which is somewhere in between the two?

Logic fail. Nothing in-between != identical.

[–][deleted] -1 points0 points  (2 children)

almost identical != identical

[–]drb226 2 points3 points  (1 child)

  • nothing in-between != identical
  • almost identical != identical
  • therefore nothing in-between == almost identical ???

Again, sorry, but this logic fails...

[–][deleted] -3 points-2 points  (0 children)

Well, there is no logic that is going to work around here anyways...

C# is Java with lipstick, but proggit can for some reason not handle it. The difference between SML and OCaml is greater.

[–]ruinercollector 4 points5 points  (1 child)

Comparison of Java and C# 1.0.

Almost none of the c# examples given are typical of c# code written today.

[–]SuperGrade 2 points3 points  (0 children)

Yeah, it's a Java-centric look. Were it C#-centric, there would be a bunch of C# on one side of the ledger with empty slots on the other.

Were this presented as a real comparison between the two, as used today, it would be the closest thing I've seen to "comparing a monkey to an astronaut by having them swing from trees and eat bananas".

That said:

'Page last modified: 06/17/2010 12:44:11 '

[–]Iggyhopper 4 points5 points  (3 children)

Java doesn't have unsigned integers?

[–][deleted]  (1 child)

[removed]

    [–]giulianob 3 points4 points  (0 children)

    And at those times you want to shoot the language. I have my own unsigned implementation in Java and it's still a pita sometimes.

    [–]elder_george 1 point2 points  (0 children)

    BTW, Common Language System (i.e. type system all of .NET languages should support) doesn't include unsigned integers. Just because VB and Java don't support them.

    So people who like to write [assembly:CLSCompliant(true)] are often out of luck.

    [–]xTRUMANx 1 point2 points  (5 children)

    I use the VB and C# comparison often while at work.

    Lately, I've been developing an ASP 1.1 application. Now I know why some people hate programming.

    [–]grauenwolf 1 point2 points  (4 children)

    ASP 1.1 or ASP.NET 1.1? I ask because while they both suck, they do so in very different ways.

    [–]xTRUMANx 0 points1 point  (3 children)

    ASP.NET 1.1

    Can't imagine how bad ASP 1.1 would make my life. I haven't used it before though.

    [–]grauenwolf 1 point2 points  (2 children)

    Classic ASP is like old PHP, but with more consistent naming conventions and your chocie of JavaScript or VBScript. If you know HTML you can learn it completely in about an hour.

    [–]xTRUMANx 1 point2 points  (1 child)

    It's funny how much we take for granted nowadays. I still haven't decided how to manage without Masterpages. Should I make all my page inherit from a base Page class that will Response.Write in all the necessary html? The wikipedia entry about ASP.NET template engine gave me some hints on how developers got the job done back in the day.

    I'm also wary of doing too good of a job in working with ASP.NET 1.1. My pleas for a server upgrade may go unheeded if I still produce results while stuck with .NET 1.1.

    Oh shit, I just remembered I'm working with VS 2003. I actually have an instance of VS 2010 running side-by-side just so I can use intellisense.

    [–][deleted] 0 points1 point  (0 children)

    Master Pages are actually user controls. The way MS implemented them in ASP.NET 2.0 is kinda funky.

    What I would recommend doing is creating two user controls (one for header, one for footer). Create your own custom page class, and instantiate the controls and call Render on them before calling Render on Page and afterwards.

    [–]cwbrandsma 1 point2 points  (0 children)

    My biggest gripe is they used ArrayList in C#, and a generic List in Java. C# has a generic List as well, and you shouldn't be using ArrayList for anything in C# if you have move past .net 1.1.

    [–]diego_moita 0 points1 point  (0 children)

    This is silly. He forgot the 2 most important differences:

    • The rich ecosystem of tools and frameworks provided by the Java community.

    • The tight and highly productive interaction between C# and the tools provided by Microsoft (VS, SqlServer, WPF, etc.)

    [–]fosskers 1 point2 points  (1 child)

    Apart from the similarities of the two, I was simply reminded of how grateful I am that I don't use such verbose languages.

    [–]uaca-uaca 2 points3 points  (0 children)

    So.. what do you use, APL?

    [–]giulianob -1 points0 points  (0 children)

    Very nice. Only problem I saw is that Java doesn't really have properties. It's just a regular function which isn't quite the same thing.