Well, while C# and Java are superficially alike there are a number of small differences that might bite you. Generally I think the opposite direction—going from Java to C#—is less problematic. This is mainly due to C# being a more complex language so you might find many simplifications from common Java patterns but the other way around might be a little painful.
Things to look out for (partial list, not guaranteed to be exhaustive):
Different ...
Naming conventions. In Java only type names start with a capital letter (i. e. PascalCase), everything else uses camelCase. Not very hard to adhere to, though.
Also interfaces generally don't start with I
. On the other hand you have to implement them with a different keyword. Doesn't really help in the middle of the code, though.
Class library :-)
While obvious, this has been the thing I spent most time on when learning a language. When dealing with a known paradigm the syntax differences are quickly sorted out, but getting to know the standard library / class library / framework takes some time in some cases :-)
- Patterns. Well, not quite, it's still the same stuff. But C# supports some patterns at the language level, while you still have to implement them yourself in Java. No events, but the Observer pattern (very prevalent in Swing—whenever you see a Listener, you know what to do :-))
Exception handling. Java has so-called checked exceptions which means that an exception must either be caught or declared upwards. Usually this means that you have
catch (SomeException ex) {
ex.printStackTrace();
}
pretty often in your code1 :-)
- Types. While .NET has normal objects and value types, they both are objects and support methods, properties, &c. Java has a dichotomy of primitive types, such as
int
, float
, char
, &c. and classes such as String
. Doesn't matter much since they implemented auto-boxing, but sometimes it's still annoying to wrap int
in Integer
.
- Polymorphism: All Java methods are
virtual
by default whereas c# methods are not.
- Minor syntactic differences.
foreach (a in b)
→ for (a : b)
- Different access keywords. Things like
internal
and protected internal
don't exist. But unqualified members are visible to other classes in the same package (sort of internal
, but then again not quite).
- String comparison isn't done with
==
in Java. You have to use .equals()
. While in C# ==
on strings is value equality, in Java ==
is always reference equality.
No ...
- Properties. In Java this is generally done with the
Foo getFoo()
/void setFoo(Foo foo)
pattern which C# generates silently behind your back when using properties but you have to do it explicitly in Java. Generally, to keep the language itself simpler many things in Java are just conventions. Still, most of the time you're better off adhering to them :-)
- Operator overloading. Deemed a hazard to the righteous programmer they weren't implemented for fear of abuse. Don't need them too often anyway, not even in C#, but sometimes they are nice and then you're missing something.
- Indexers. You always have to access list items through
myList.get(5)
instead of the array-like syntax myList[5]
. Just a mild inconvenience, though.
- LINQ (though there exist implementations2 but it's not as nicely integrated), or lambda functions3 (no delegates anyway, but anonymous classes), extension methods, or partial classes (yes, that's a painful one when dealing with Swing, unless you're very disciplined), and a few more things.
- Multidimensional arrays. You can use jagged arrays (arrays of arrays), buttrue multidimensionality isn't there.
- Generics are compile-time only, at runtime only
Object
s remain. Also wildcards in generics can be hard to resolve sometimes when the compiler complains about all of the four ?
in your generics having different types. (Though to be fair: That was a case where I would have needed type information at runtime anyway so I reverted back to Object
s).
General advice: Grab a friend with Java experience and let him glance over your code. While he probably can't tell you everything you should take care of when you directly ask him that question, he can spot strange things in code just fine and notify you of that. This has greatly helped me learning Java (although I learned Java first and then C#, so it might be different).
1 Yes, I know many catch blocks look different, but still, this is probably the archetypical one and not even that rare.
2 Quaere, JaQue, JaQu, Querydsl
3 There's lambdaj, though. Thanks for pointing that out, Esko.