Here is the basic Hello World program.
Save this file somewhere on your hard drive with the name Hello.cs. I am using the name of the class as the name of the file but, unlike Java, that does not have to be the case. The SDK installs an item on the Start Menu that is a command prompt with system variables set up correctly. Bring up this command prompt. Change directory to where you have saved the file and compile the C# class with the command
This generates an executable file called Hello.exe, which you can invoke from the command line. It will print out the text "Hello World".
The program looks very similar to Java and other C-like languages. The main difference is in the set of
tools and class libraries. For example, System.Console is used rather than
System.out in Java.
The entry point to C# programs is the Main method, which must be static. This example has
a void return type, which means that it doesn't return anything. The
Main method can also have an integer return type and can take command line arguments.
C# is compiled into bytecode and then executed in a stack based virtual machine. The other .NET languages Visual Basic .NET, CLI, and J# are also executed in the virtual machine in a similar way although Visual Basic does not need to be compiled first. Common Intermediate Language (CIL) (formerly called Microsoft Intermediate Language or MSIL) is a human-readable language resembling an object oriented assembly language that you will often see referred to in the literature, usually for the purpose of explaining the workings of the Microsoft virtual machine.
The program above uses the WriteLine() method of the Console class to output
text to the console. Console is a C# built in application programming interface (API).
Console is a part of the System namespace. Let's extend this example a little
further to explore some of the other options mentioned. The source file is in
ClArgs.cs.
Compile and run the program and run it as before. You will need to append some program arguments to get anything printed out.
There a couple of new things here. The first line of the program includes the using
directive to avoid having to type the fully qualified name of the Console class
later on. The command line arguments were passed into the Main method and then printed back out
to the console. The output method passed the arguments listed after the string literal to the
WriteLine method to format in the string. Finally, a value of 0 was returned from the program.
There are no user comments.
Please send ideas and opinions by email at alexamies@gmail.com.