Switching from Java to C#

Started by
12 comments, last by Xai 11 years ago

I'm planning to switch from Java to C#, does anyone have any tips for any differences I need to watch out for? Naming conventions, things I need to implement differently, ect?

Thanks.

Advertisement

Well, you'll need to learn the base class library obviously. I don't know Java that much but from memory I remember:

- All method names have a capital as their first letter.

- New: properies
- No checked exceptions

take a look at this for feature comparison: http://en.wikipedia.org/wiki/Comparison_of_C_Sharp_and_Java

Many little differences.

  • switch statements are fixed. No implicit fall-through.
  • Importing happens on a per-namespace basis (no importing single classes). No static import in C#.
  • No covariant return types in C# (i.e. a method returning object cannot be overridden by a method overriding string, like in java)
  • C# offers structs, i.e. objects with value semantics (copied when passed as argument or return value instead of by reference, etc.)
  • IEnumerable<T> is the central collection class.
  • Generics are not half broken in C#. They don't rely on type erasure: you can create an array of T[]. They work on all types: you can have a list of ints. Contravariance and covariance are supported: an IEnumerable<string> is a subclass of IEnumerable<object>. Overloading is possible: K<T> and K<T, U> can both be defined.
  • Methods are nonvirtual by default. If you want to be able to override them, they need to be declared either abstract or virtual.
  • When an abstract class implements an interface, all methods need to be either implemented or declared as abstract methods.
  • Using 'override' in C# is not optional like @Override.
  • C# has 'yield return', letting you lazily generate IEnumerables.
  • LINQ
  • C# has no "inner classes", only "nested classes". So, declaring a class within a class in C# is like declaring a static class inside another class in java.
  • C# offers properties (replace getters/setters), events (observers), delegates (first class methods), anonymous lambdas (inline functions). There is no final-only limitation on which variables can be captured by closures.
  • Access modifiers work slightly differently. C#'s 'protected' is different from java's. There's also 'internal'.
  • Operator overloading.
  • Extension methods.
  • Keyword parameters.
  • Default argument values.
  • Verbatim strings (which make regexes readable again).
  • ...
On top of what Sam said, exception handling is much more relaxed in C#, the design of the library is a lot more logical, generics aren't a hack job and pInvoke is about 100x times better than JNI. You also have a bit more low level control in unsafe code.

Plus no Maven! &$$;$$ Maven!

Generally I find c# to simply be a better thought out Java, which makes sense as it came later.
Oh yeah, forgot a big difference. ref and out. Java is pass by value, c# allows pass by reference. Also struct is a value type in C#.

stein102,

I am just curious about why you are switching from Java to C#. An expert Java programmer and two expert C# programmers that I know all like C# better but all admit that Java is very capable and still widely used. When is comes to OS and hardware cross-platform implementations, it is a strong point that many believe Java is better in this way. In the business world, preference of the coder is sometimes not even a consideration in established development firms.

So, what are your reasons for switching? Perhaps we can clarify some things that you need to know by this insight given to us.

Personal life and your private thoughts always effect your career. Research is the intellectual backbone of game development and the first order. Version Control is crucial for full management of applications and software. The better the workflow pipeline, then the greater the potential output for a quality game. Completing projects is the last but finest order.

by Clinton, 3Ddreamer

stein102,

I am just curious about why you are switching from Java to C#. An expert Java programmer and two expert C# programmers that I know all like C# better but all admit that Java is very capable and still widely used. When is comes to OS and hardware cross-platform implementations, it is a strong point that many believe Java is better in this way. In the business world, preference of the coder is sometimes not even a consideration in established development firms.

So, what are your reasons for switching? Perhaps we can clarify some things that you need to know by this insight given to us.

Well, been programming in java for a while and recently been using Slick2D for programming games. I've been doing some reading really like the looks of XNA.

Another reason being that I'm a pre-college student and want to have a couple languages down before I hit the workforce. I was considering going from Java to C# or C++, I'm still kind of torn between the two languages, that's one of the reasons I made this thread. I wanted to see the differences between Java and C# so it could help me make my decision making a little easier.

Since you are doing this for the learning, obviously to prepare for a career, then I say more power to you!

Take a look at MonoGame implementation of XNA. There is a ton of things to get with it, including terrain editors and level editors.

Also look at Unity 3D, which would give you the opportunity to use any of several languages, including C#.

This is a good strategy for getting broad experience which you are taking. Long term you need to complete good projects for your portfolio

Personal life and your private thoughts always effect your career. Research is the intellectual backbone of game development and the first order. Version Control is crucial for full management of applications and software. The better the workflow pipeline, then the greater the potential output for a quality game. Completing projects is the last but finest order.

by Clinton, 3Ddreamer

Since you are doing this for the learning, obviously to prepare for a career, then I say more power to you!

Take a look at MonoGame implementation of XNA. There is a ton of things to get with it, including terrain editors and level editors.

Also look at Unity 3D, which would give you the opportunity to use any of several languages, including C#.

This is a good strategy for getting broad experience which you are taking.

I still haven't fully decided between C# and C++ yet(I plan to learn them both eventually), but I'm slightly leaning towards C++ with either SDL or SFML. I have a few books for C++ that I picked up a while ago on sale also.

Either language would be fine, C# or C++, but you really need to look at how low you want to go. Are you serious and dedicated to learn lower level programming under the graphics, threading, sound, input, and so forth? If that is the case, then I would say C++. An intention to stay middle and high level coding for a while might give you C# as the best interim choice and you can sit on an engine like XNA/MonoGame for a while to sharpen your coding skills in preparation for that advanced lower level coding. Please don't misunderstand because any of these - Java, C#, or C++ - can be used to target a runtime and hence manipulate lower level CPU/GPU functions, but I feel that you should wait 1-2 years before you dive that deep.

Personal life and your private thoughts always effect your career. Research is the intellectual backbone of game development and the first order. Version Control is crucial for full management of applications and software. The better the workflow pipeline, then the greater the potential output for a quality game. Completing projects is the last but finest order.

by Clinton, 3Ddreamer

This topic is closed to new replies.

Advertisement