views:

278

answers:

5

This is a question for anyone who has the pleasure to work in both Java and C#.

Do you find that you have to make a mental context switch of some kind when you move from one to the other?

I'm working in both at the moment and because the syntax and libraries are so similar and yet subtly different I'm finding it frustrating when i move from one to the other.

This is more so than I've experienced moving between any other programming languages.

Does anyone have any tips for making your brain work differently for languages that are so similar?

+6  A: 

Yes, I have to make a mental context switch - because LINQ isn't available in Java :(

Beyond that, there are little things like foreach (X x in y) vs for (X x : y) which often trip me up, but not a huge amount otherwise.

As for a tip to make your brain work differently: don't give into the temptation to use the naming conventions of the other language. I find that using camel-cased method names in Java is a good nudge to my brain, for example.

EDIT: The differences in terms of generics are important and can occasionally be brain-warping, too.

Jon Skeet
Congrats, you just answered this question twice in a short amount of time :), DotNetRocks listeners know what I'm talking about ;-)
Pop Catalin
+3  A: 

I find that most difficult switches are around generics (which C# does better), delegates (which Java sort of hacks with anonymous inner classes) and the events (which Java doesn't have)

I find that making sure you are using the idioms of the language (i.e. using delegates instead of creating an interface in C#) will help snap your mind into gear.

cynicalman
+2  A: 

They are similar enough that the differences trip me up. I learned Java in college and starting using C# when I got my first programming job.

C# in .Net 1.1 seemed a lot more similar, but now in C# 3.0, I use delegates, generics and especially API constructs like Dictionaries, way more than I ever did in Java. I've used the functional aspects of C# more and more as I work my way through learning F# (now writing in that or OCaml really forces me to change my way of thinking).

Tony Peterson
+3  A: 

The langauge differences as others have mentioned is the most obvious and problematic. I too find the library differences maddening at times and really wasting a lot of time.

The "details" can really get you as well, such as understanding how the memory model works or the way your runtime performs optimizations. Having intimate knowledge of the runtime environment's memory model, garbage collection techniques, threading model, etc can create significant changes in the way you think and develop software.

I cannot intelligently compare Java vs C#'s details at that level, but I can say that many things I would do in Java, I am uncomfortable or unsure if I can do in C# because I don't understand its low-level details. This affects code I write for everything from GUI-worker interaction to dealing with memory management.

I have found the best way to cope with the differences in Java and C# is to simply think of them as complete different languages--avoid the trap of "C# and Java are basicaly the same language with different class names".

James Schek
+3  A: 

The difference in how string equality comparisons work between Java and C# is one thing that needs to be kept in mind, particularly when moving from working in C# to working in Java.

In C# you can do a value comparison on two string instances with the == operator:

// C# string value comparison example
string string1 = GetStringValue1();
string string2 = GetStringValue2();

// Check to see whether the string values are equal
if (string1 == string2) 
{
    // Do something...
}

In Java, the == operator does a reference (not value) comparison on strings, so you need to use the equals() method:

// Java string value comparison example
String string1 = getStringValue1();
String string2 = getStringValue2();

// Check to see whether the string values are equal
if (string1.equals(string2)) 
{
    // Do something...
}

Bottom line: When comparing strings in Java, don't forget that operator == doesn't do a value equality comparison, like it does when comparing strings in C#. (The same concept also applies to operator !=.)

Jon Schneider