tags:

views:

179

answers:

4

I am having to port code from Java to C# (and soon the other way round) by copying and pasting and then editing compiler errors. (Please accept that this is necessary; I can explain if required). Are there cases where the same code fragment runs differently in the two languages? I would include cases where order or operation or operator precedence was defined in one languge but undefined in the other but exclude cases where it was undefined in both.

I would be particulary interested in any development tools which could identify such fragments.

@Thomas. Thanks. J# is not an option - the code is mandated to be in C#. What does "this" refer to in your comment?

+7  A: 

One example - Java runs variable initializers after the superclass constructor. C# runs them before. This affects things if the superclass constructor then calls a virtual method overridden in the class in question. (This is generally a bad idea, but it happens.)

Then of course there's generics, which are very, very different. For example:

public class Foo<T>
{
    static int counter;
}

In Java there's one counter variable. In C# there's one per constructed type, so Foo<string>.counter and Foo<int>.counter are independent.

I'm afraid I don't know of any tools to identify this kind of thing.

I've done a certain amount of porting Java to C# myself, and I think it's important to try to make the resulting code as idiomatically "sharpy" as you can. Things like converting getters and setters to properties and sometimes indexers, for example. Implementing IDisposable where appropriate... things like that.

Jon Skeet
+1 thanks Jon. I agree generics should be excepted from the discussion
peter.murray.rust
If you have used a good naming schemes for getters and setters, likegetName() and setName(), then Visual Studio .Net converter automatically creates properties for them. That one thing I liked.Also if you are using classes like Sockets, the behaviour can be different in different situations.
cornerback84
+2  A: 

Perhaps the Java Language Conversion Assistant Wizard could assist you here?

http://msdn.microsoft.com/en-us/library/7tatw8a2%28VS.80%29.aspx

In addition I would suggest something like Resharper or another refactoring tool to take advantage of the C# language features and make your code more efficient and readable.

edit:

V3 is available with VS2005, read this if you dont seem to have it installed: http://natarajana.com/jlca.aspx

Alex
@Alex. Thanks, I will see what the quality of generated code is and it may be a partial solution.
peter.murray.rust
This will highlight most of the conversion issues where system APIs are concerned -- but there are still some subtle issues to be aware of. For example the JVM Thread class can be subclassed, but the .Net Thread class is sealed; the conversion assistant will create a wrapper object around a Thread where the Java has a Thread subclass -- there will be subtle differences in behaviour if code assumes that the wrapper is the Thread.
Steve Gilham
@Peter: That is why I suggested a refactoring tool such as Resharper. Out of the box Resharper is fantastic for automatically detecting and cleaning up sloppy code that something like an automatic code generator might produce. If you find a common theme that isnt automatically corrected it is fairly easy to add custom refactoring to the Resharper configuration. I'm sure other tools can do this too, I just like ReSharper.
Alex
@Steve: very good point, I didnt think of that.
Alex
@Alex. Thanks - my colleague is badgering me to get Resharper as well! Until then how would I access Java Language Conversion Assistant Wizard - I have Visual Studio installed but I can't find it under "File | Convert" as the MSDN docs suggest. Does it have to be purchased?
peter.murray.rust
@Peter. I've modified my original answer to provide some links.
Alex
+4  A: 

Converting from Java to C# is difficult not only because of language difference, but because of not compatible base libraries. As for me, the best way to convert from Java to .Net is using IKVM - an implementation of Java for Mono/.NET

macropas
Thanks, this looks useful.
peter.murray.rust
A: 

IKVM is a great option for running java code in C#. You don't need to modify your java code. It simply takes a jar file and generates a .NET dll.

http://www.ikvm.net/

Going the other direction, from C# to java, will be difficult, as C# is basically a superset of java. You would need to limit the use of constructs which are not available in java.

turtlewax