Console applications in C#


Here is my first console application in C#, an orthodox “Hello World” example to start with.

   1: /*
   2:  * hello.cs
   3:  * Simple Hello World program in C#
   4:  *
   5:  * @author achindra
   6:  * @version 0.1
   7:  */
   9:  using System;
  11:  class Hello
  12:  {
  13:     static void Main()
  14:     {
  15:         Console.WriteLine("Hello World!n");
  16:     }

17: }


System is a namespace which has Console class that we are referring in this program to call a function WriteLine that prints message to console window. This is equivalent to printf in C. Also note that main is defined as static in class Hello. Static qualifier ensures that the method Main is with Class and not with an instance of it and thus entry point to a console application is always static method Main. Unlike C, Main is not declared global. In C# methods and variables are not supported at global level.

From the Visual Studio command window, compile a C# code file into a binary with CSC.exe (C# Compiler)

C:UsersAchinBhaDocumentscodeCShello_console>csc hello.cs
Microsoft (R) Visual C# 2010 Compiler version 4.0.20506.1
Copyright (C) Microsoft Corporation. All rights reserved.
 Volume in drive C has no label.
 Volume Serial Number is 1CE3-DB2A
 Directory of C:UsersAchinBhaDocumentscodeCShello_console
14-08-2009  21:39    <DIR>          .
14-08-2009  21:39    <DIR>          ..
14-08-2009  21:37               213 hello.cs
14-08-2009  21:39             3,584 hello.exe
               2 File(s)          3,797 bytes
               2 Dir(s)   7,285,137,408 bytes free
Hello World!


Technorati Tags: ,,

I am a curious mind, always building and experimenting, trying things to decode their inner design. Kind of a small scale inventor ;-) I like to help people realise their passion.

Leave a Reply

Your email address will not be published.