识别编译Java和C#,但运行不同的代码 [英] identifying code that compiles in both Java and C# but runs differently

查看:124
本文介绍了识别编译Java和C#,但运行不同的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过复制和粘贴,然后编辑编译器错误不必从Java代码移植到C#(很快反过来)。 (请接受,这是必要的;如果需要,我可以解释)。有没有情况下,相同的代码片段在两种语言中运行不同?我的地方包括命令或操作或运算符优先级在一个languge被定义,但未定义在其他情况下,但不包括它在这两个不确定的情况下。

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。谢谢。 J#是不是一种选择 - 代码的任务是在C#。 ?什么是本,在您的意见,请参照

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

推荐答案

一个例子 - Java的运行变量初始化的之后超类构造函数。 C#之前运行它们。这会影响的事情,如果超类构造函数调用,然后在有问题的类中重写虚方法。 (这通常是一个坏主意,但它发生。)

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;
}

在Java中的有一个计数变量。在C#中有每个构造类型之一,所以美孚<串> .counter 美孚< 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.

我已经做了一定量的Java移植到C#我自己,我想尽量让生成的代码,因为它是重要的惯用的夏氏,你可以。比如像getter和setter转换特性,有时索引,例如。实施的IDisposable 在适当情况下......这样的事情。

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.

这篇关于识别编译Java和C#,但运行不同的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆