Arne said:
Not counting end brackets and the main program either I assume.
You assume wrongly, seven lines comprise a complete working example that
runs in Eclipse
Well, since you seem so interested, I'll post it here, then you can have
the satisfaction of telling me where I am going wrong
.
I didn't post it earlier in order to avoid discouraging newbies from
thinking for themselves a little.
Any conversion of C# to Java could be done at many levels, statement by
statement would be one. Producing the same output would be another.
Somewhere in between there are a range of conversions which, to varying
degrees, employ Java idioms or features in place of C# idioms or features.
Recall, in my original post I said ...
"What purpose is served by MyClass that couldn't be better achieved by
using String[] in place of MyClass in MyClient below?"
Well I chose a level of conversion which assumed that there was no real
need for MyClass in order to produce the same output. Maybe MyClass is
unnecessary in C# also but was included for ulterior reasons (e.g. to
demonstrate some aspect of C# programming) in which case my conversion
could be regarded as "incorrect". The OP doesn't provide enough
information to decide.
So, excluding blank lines and comments, here are the 7 lines I came up
with ...
-----------------------------------------------------------------------
class MyClient {
public static void main(String[] args) {
String[] mc = {"Rajesh","A3-126","Snehadara","Irla","Mumbai"};
System.out.printf("%s,%s,%s,%s,%s,%s",
"{0},{1},{2},{3},{4}",mc[0],mc[1],mc[2],mc[3],mc[4]);
}
}
-----------------------------------------------------------------------
There you are, elementary stuff, nothing clever or surprising. A bit
shorter than the OP's C# original even accounting for the different
conventions for opening brackets.
Note that I did not, and am not, claiming that an equivalent program
couldn't be written in seven or fewer lines in C#. I merely noted that
my conversion produced 7 lines of Java.
-----------------------------------------------------------------------
using System;
using System.Collections;
class MyClass
{
private string []data = new string[5];
public string this [int index]
{
get
{
return data[index];
}
set
{
data[index] = value;
}
}
}
class MyClient
{
public static void Main()
{
MyClass mc = new MyClass();
mc[0] = "Rajesh";
mc[1] = "A3-126";
mc[2] = "Snehadara";
mc[3] = "Irla";
mc[4] = "Mumbai";
Console.WriteLine("{0},{1},{2},{3},{4}",mc[0],mc[1],mc[2],mc[3],mc[4]);
}
}
------------------------------------------------------------------------