From Java To C# – Hello World

Today I went back to Core Java book, and during seeing (again!) the hello world sample I wondered, how many steps I need to make it C# valid code. So let’s take a look on the java code:

public class Welcome
{
   public static void main(String[] args)
   {
      String[] greeting = new String[3];
      greeting[0] = "Welcome to Core Java";
      greeting[1] = "by Cay Horstmann";
      greeting[2] = "and Gary Cornell";

      for (String g : greeting)
         System.out.println(g);
   }
}
  1. Firstly, we need to know, that we have String class name in C# too, but to use that we need to use namespace too (System.String). To still have one word name, we need to use string alias.
  2. To fulfill compiler requirements (he is looking for Main method, it cannot be main, no way …) we need to rename our static method from main, to Main. That’s the entry point in C# which compiler is looking for.
  3. C# contains different syntax for the foreach statement, we need to use foreach (string g in greeting). By the way, that’s interesting why when foreach was introduced in Java 5 (they used concept known from C#), the creators of Java language didn’t decide to use the same syntax as C# is using. It’s more verbose and easier to read, but that’s probably only my opinion 🙂
  4. Finally, the API to print the string (to console) is different on both platforms , so we need to use System.Console.WriteLine(g);
  5. The file name for Java matters (public Welcome class needs to map to Welcome.java file name, for C# you can name the file as you want – good practice is to follow the same way as Java). I think that would be beneficial for C# to have the same approach. (It sometimes happen that file name is totally unconnected with the entry, because someone could forget that he renamed class, moved class or whatever, but compiler is not helping in such a situation. Fortunately, ReSharper is helping a little 😉
  6. Last interesting thing which is not directly connected to sources, but it is still showing interesting differences between platforms is how we are compiling and running the programs from command line. For java, we are compiling source code with javac, and similarly we have with C#, we are compiling source file with csc. The difference appears during running the program, for java – java <program_name>, for C# just <program_name>.exe.

Our final C# source code you can see below:

public class Welcome
{
   public static void Main(string[] args)
   {
      string[] greeting = new string[3];
      greeting[0] = "Welcome to Core Java";
      greeting[1] = "by Cay Horstmann";
      greeting[2] = "and Gary Cornell";

      foreach (var g in greeting)
         System.Console.WriteLine(g);
   }
}

Firstly I thought they shouldn’t be very different (I used both languages, but I never compared them directly so I thought they are looking very the same). In general I can say that’s true, because source codes are looking similar, but when it comes to details it is surprising that in such a simple program we have this amount of differences in source codes.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s