|
|
|
|||||
|
14 |
How do I build a C# application using Command Line Compiler? |
|
||||
|
|
||||||
| Posted - February 05, 2005 | ||||||
|
Microsoft’s .NET SDK ships with a command line compiler called csc.exe.
It can be executed from within the DOS prompt. In this FAQ, 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 14.1 Listing 14.1 001: // HelloWorld.cs 002: // ------------- 003: using System; 004: class HelloWorld 005: { 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.cs If you have installed Mono C# Compiler then you should compile the above program using the following command: mcs HelloWorld.cs If 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. For example, you have
to give the following command for executing the above program. |
||||||