Microsoft’s .NET SDK ships with a command line compiler called csc.exe. It can be executed from within the DOS prompt. In this article, I will show you how to build a simple C# program using this compiler with Notepad as the editor.
Open your editor and enter the code as given in listing 1
Listing 1
001: // HelloWorld.cs002: // -------------003: using System;004: class HelloWorld005: {006: public static void Main()007: {008: Console.WriteLine("Hello World");009: }010: }The line numbers are given only for the sake of explanation and does not form part of the source code.
In the above listing, Line 3 defines the namespace System. Line 4 declares our class named HelloWorld. Line 6 defines the Main() method, which is considered as a entry point for all C# programs. Line 8 calls the WriteLine() method of the Console class and prints "Hello World" as output.
Save the file as HelloWorld.cs and compile the code using a C# compiler. I assume you are using the compiler which ship with .NET SDK. For this purpose, you have to give the following command at the DOS prompt as shown in the figure given below
csc HelloWorld.csFigure 1
If you have installed Visual Studio 2008, you can compile a C# program using Visual Studio Command Prompt. You can launch it from Start | All Programs | Microsoft Visual Studio 2008 | Visual Studio Tools
If you have installed Mono C# Compiler then you should compile the above program using the following command:
mcs HelloWorld.csIf there are any errors and warnings, the compiler will display them during the above process. You have to go through all those messages and correct them preferably by going back to the source code. As explained earlier, C# is a case sensitive language and hence even if you miss a semicolon or a comma, the compiler will throw error messages. If there are no errors and warnings your screen would look like as shown in the figure given above.
To view the output of the above program, you have to supply the name of the assembly (HelloWorld.exe) at the DOS prompt.
For example, you have to give the following command for executing the above program.
HelloWorldThe output will be as shown in the figure given below
Figure 2
For mono compiler, the execution statement will be as shown below:
mono HelloWorld.exeAn Assembly is a file which is created by the compiler upon successful compilation of every C# application.
Commenting the Code
If you look at line numbers 1 and 2 in the above code, you would see two slash lines at the beginning. In programming terminology, these lines are called as comments. C# compiler won’t compile the statements inside these comments and they are given only for documentation and reference purposes. It is a best practice to give comments while coding as it will help you to study the code at a later stage or for others who look at your code.
There are three ways by which you can give comments in C#. The first two will be already familiar to you if you had worked with C++ and Java. They are single-line and multiline comments.
Single-line comments are given with the symbol //, while Multiline comments are applied with /*…..*/ symbols and can spread more than one line (See Listing 2)
Listing 2
// this is a single line comment/*This is aMultilineComment*/The third type of comment which is given with “///” is a new one introduced by the .NET Framework. It is used to generate documentation for your C# program and they are denoted by using the relevant XML tags as shown in listing 3:
Listing 3
/// <summary>/// this is a new comment system/// <summary>
[tag]microsoft, c#, java [/tag]
good artilcle
Nice articles.
The design of this page is not looking good in IE 7.0.
Anyone know how to do things like this?