In this question C# constructor execution order, the main answer mentioned that
Note that in Java, the base class is initialized before variable initializers are run. If you ever port any code, this is an important difference to know about
I want to know that are there real examples that you can't simply port between C# constructor and Java constructor because of this issue?
If so, are these examples only deliberately designed anti-patterns or do they exist in real projects like some open source projects?
Update: Can we make a list of patterns that can't be ported simply, e.g. prove that if there are no such patterns in the code, the constructor can be mapped simply with tools.
My try (with help from @John): according to a comment of that answer, the code bellow can't be ported simply, but it is deliberately designed:
C#:
class foo
{
public foo()
{
if (this is bar)
{
(this as bar).test();
}
}
}
class bar : foo
{
string str="str" ;
public bar()
{
}
public void test()
{
string s=str.Substring(1);
}
}
Java:
static public class foo
{
public foo()
{
if (this instanceof bar)
{
((bar)this).test();
}
}
}
static public class bar extends foo
{
String str="str" ;
public bar()
{
}
public void test()
{
String s=str.substring(1);
}
}