• Post Reply Bookmark Topic Watch Topic
  • New Topic

Moving from Java to C#  RSS feed

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings, thank you so much for passing by

So I want to move on and start learning C#. However, I know how to code in Java, So I would expect that might helps me a lot.
But I don't want to learn again all the basic stuff (Classes, Methods, Objects, Interfaces, arrays, loops, etc...) again... because that is going to be waste of time.
any advice on how should I start?
because it seems that the only struggle that I have right now is writing the codes in C# language, because I noticed that it's slightly different from Java.
excuse my weak English, I am not a native English speaker.
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
C# is similar to Java, but both languages also have some features that are unique to each of them.

If you know how to program in Java, then it will not be hard to learn C#. You won't have to learn about classes, methods, objects, arrays, loops etc. from scratch, because it's all the same in C# as in Java, just with slightly different syntax sometimes (and a lot of the times it's the same).

The hardest thing in my experience of programming in C# is the difference between the standard libraries - there are lots of similar classes in the Java and C# standard libraries, but they have different names and different methods.

If you want to learn more C#, then practice - write small programs, experiment, and keep a reference manual at hand to lookup things if you don't know them.
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is necessary to learn the difference from Java®. For example C# doesn't use inner classes (I think) and has things called delegates instead. You may think that is a minor difference, but wait until you have to add an action to a button on a GUI; you will be using delegates all the time.
 
Saloon Keeper
Posts: 7993
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some things you could google for, off the top of my head:

- delegates.
- events.
- (auto-)properties.
- structs (I like to avoid using them though).
- operator overloading.
- enums are really ints (booooo).
- IEnumerable<T> is like Java Iterable<T> and Stream<T> rolled into one (awesome!!!).
- IEnumerables may be lazily returned from methods using the 'yield' keyword.
- 'using' is like 'try-with-resources', but not quite.
- no checked exceptions.
- generics may take primitive type arguments (e.g. IList<int>).
- local variables may be declared with 'var'.
- You may explicitly implement interface methods, which causes them to be inaccessible through the sub-type's API.
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the last item, here's an example. Let's say you implement the IDisposable interface (which is C#'s equivalent of Java's AutoCloseable). You have to implement the Dispose() method, but you would like to call it WrapThingsUp() instead, because it more closely resembles the nomenclature used for say... a house party:

 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another BIG difference:

In Java, all methods are implicitly 'virtual', meaning they can be overridden. In C#, only abstract methods (in interfaces/abstract classes) are implicitly virtual. If you declare a method in a concrete class, and you want sub-classes to be able to override it, you must explicitly declare it as virtual:

 
Ranch Hand
Posts: 57
3
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:...For example C# doesn't use inner classes (I think)...


This isn't correct. C# has inner classes, they just aren't used often, but you can "nest" them for some additional flexibility (more on that here.)

Delegates are interesting, and I still struggle with explaining how they work today, so make sure you check out Microsoft's article on the subject. The best way that I can personally compare them to java, is that they work similar to interfaces that are used for observables.
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Harris wrote:This isn't correct. C# has inner classes


I think this is where the jargon is very confusing. C# has nested classes, but not inner classes. In Java, "inner class" means non-static member class. In C#, member classes are always static.

Delegates are interesting, and I still struggle with explaining how they work today, so make sure you check out Microsoft's article on the subject. The best way that I can personally compare them to java, is that they work similar to interfaces that are used for observables.

You can see a delegate as a "method signature declaration". Having a delegate as a method parameter essentially says: "Give me a handle to a function that conforms to this signature". You can pass the name of a method, a 'new delegate', or a lambda.

Delegates are pretty much the same as Java 8 Functional Interfaces.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote: . . .
- generics may take primitive type arguments (e.g. IList<int>).
. . .
I thought that more recent versions of C# had dispensed with primitives completely, and what appears to be a primitive is in fact an object. Maybe I am mistaken.
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not that I'm aware of. I know you can do stuff like:

I imagine that this is just syntactic sugar though. I really don't know how C# works under the hood.
 
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int, long, etc are just shorthand for Int32, Int64 and several others. You can even write String and Object with all lower case.

What I missed in this discussion is extension methods:
I actually want something like this to be created for Java, but the way Java works makes this hard. Whereas in C# all you need to do is "import" the package containing the class containing the extension method, this is trickier in Java because of the way class loading works.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not so sure that I like C#'s extension methods. I've worked on a C# codebase with extension methods, and it was sometimes hard to understand what was happening. Somewhere a method would be called, and when I looked in the source of the class the method was not there. Instead it was defined as an extension method somewhere else. This doesn't make it easy to understand the structure of a large codebase.

Even Microsoft recommends:
In general, we recommend that you implement extension methods sparingly and only when you have to. Whenever possible, client code that must extend an existing type should do so by creating a new type derived from the existing type. For more information, see Inheritance (C# Programming Guide).

 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's where an IDE can come in. For example, Eclipse could just let you Ctrl+Click on the method and the extension method opens. But I agree that they aren't really necessary; I just prefer them when I add utility methods for classes like StringBuilder. Because of the static utility method I must break chaining, or write hideous constructs:
Out of these, I would prefer the first over the other two. (I never use the second in real life, it's just hideous and error-prone.)

I dislike the notion of extending a class just to add some utility methods. Sometimes that's not even possible - if the class is final (e.g. String) or is returned by third party libraries.
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also have mixed feelings about extension methods. Finding documentation about them is difficult, but they make some APIs so much more sexy.

If I could extend Java's Stream class with my own operations without subclassing, that would be amazing.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!