• Advertisement
Sign in to follow this  

Switching from Java to C#

This topic is 1731 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement

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).
  • ...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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#.

Share this post


Link to post
Share on other sites

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.

Edited by 3Ddreamer

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Edited by 3Ddreamer

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Edited by 3Ddreamer

Share this post


Link to post
Share on other sites
Going from Java or C# to C++ you will find yourself saying "I can't do what?!?!" A lot. The c++ standard libraries are shockingly limited. Hell, even a standard string class or dynamic array class are fairly new to the standard. Things like graphics, ui, sound, database, directory manipulation, networking, etc... Zip, zilch nada. Thankfully there are frameworks like Qt, but that's almost like learning a whole other language.

Share this post


Link to post
Share on other sites

Well, that's a good point.   Frameworks might weigh more heavy in deciding between C# or C++ than anything else, which really is a matter of preference.

Share this post


Link to post
Share on other sites

Going from Java or C# to C++ you will find yourself saying "I can't do what?!?!" A lot. The c++ standard libraries are shockingly limited. Hell, even a standard string class or dynamic array class are fairly new to the standard. Things like graphics, ui, sound, database, directory manipulation, networking, etc... Zip, zilch nada. Thankfully there are frameworks like Qt, but that's almost like learning a whole other language.

 

So I'll need to download additional libraries even for simple stuff? I mean, I'm open to learning, I just want to know what I'm getting into. (Referring to C++)

Share this post


Link to post
Share on other sites

Going from Java or C# to C++ you will find yourself saying "I can't do what?!?!" A lot. The c++ standard libraries are shockingly limited. Hell, even a standard string class or dynamic array class are fairly new to the standard. Things like graphics, ui, sound, database, directory manipulation, networking, etc... Zip, zilch nada. Thankfully there are frameworks like Qt, but that's almost like learning a whole other language.

 

So I'll need to download additional libraries even for simple stuff? I mean, I'm open to learning, I just want to know what I'm getting into. (Referring to C++)

 

Yes, you will be using 3rd party libraries in C++ in ANY program beyond a 2 week school lab.  In the other 2 platforms, you will be using 1 of many libraries as well, but the differences are:  

 

1.  The C++ Standard Library has about 20% as much core functionality in it (no DB access, no XML processing, no graphics, no sound, no threads, no web technogy, URI, no control libraries of any kind, no UI building, etc, etc.) - because the C++ standard library makes almost NO assumptions about platform.  It works for writing programs with a graphical UI, a console interface, an embedded factory robot control board, etc.  It is primarily intended to be only the set of things widely needed and NOT readily build-able with itself (in other words, if you could easily add the library piece by writing it using the existing library piece and nothing else, then it doesn't have to be in the standard, you can download it elsewhere - unless it is so common and needed that leaving it out would cause nearly everyone to need to add / download it).

 

2.  The Java and .Net libraries gave a LOT of thought to trying to be complete platforms, such that many programs could be built without needing third party software.  However, third party libraries are still better at many things (such as Log4J, Hibernate, etc).  Its just that in Java and .NET they are trying to be better than something, rather than provide it in the first place.

 

3.  Java and .Net were built from the ground up to provide a clean example and guideline for class and library writers to follow.  They include not just specifications, but naming convention recommendations and examples of library organization and packaging.  C++ was built in a world where C professionals already had multiple entrenched competing styles, and often big monolithic projects and C++ didn't try to force people to change - it is just a tool.  So C++ libraries have been completely inconsistently named and organized from the beginning.  Some of the best and cleanest code in the world is C or C++ - but a lot of it is completely unreadable by anyone at all.

 

4.  The biggest issue with C++ libraries, is that, since the core is so spartan, almost every vendor there own copy of "basic" helpful classes, which means that when you have 3 3rd party libraries you have 3 different sets ... 1 inside each vendors code ... and each one is going to be different and not compatible across libraries.  So for instance, logging of memory management or dependency injection are just going to be completely tied to each library.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement