How do I build a C# application using Command Line Compiler?

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

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">001: // HelloWorld.cs
002: // -------------
003: using System;
004: class HelloWorld
005: {
006: public static void Main()
007: {
008: Console.WriteLine(&quot;Hello World&quot;);
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

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">csc HelloWorld.cs 

Figure 1

c#, visual c#, visual studio, mono

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:

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">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.

READ  ASP.NET 4 and Visual Studio 2010 Released

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.

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">HelloWorld 

The output will be as shown in the figure given below

Figure 2

c#, visual c#, visual studio, mono

For mono compiler, the execution statement will be as shown below:

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">mono HelloWorld.exe 

An 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

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">// this is a single line comment 
/* 
This is a
Multiline 
Comment 
*/ 

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:

READ  OLX.IN - The Ultimate Choice for Sellers and Buyers

Listing 3

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">/// <span style="color: #0000ff">&lt;</span><span style="color: #800000">summary</span><span style="color: #0000ff">&gt;</span>
/// this is a new comment system
/// <span style="color: #0000ff">&lt;</span><span style="color: #800000">summary</span><span style="color: #0000ff">&gt;</span>

 

[tag]microsoft, c#, java [/tag]

3 Responses to "How do I build a C# application using Command Line Compiler?"

  1. pons@vb gridview treeview asp.net   July 22, 2009 at 10:05 am

    good artilcle

    Reply
  2. Cherukuri Venkateswarlu   December 31, 2009 at 5:45 am

    Nice articles.
    The design of this page is not looking good in IE 7.0.

    Reply
  3. JamesDX   February 15, 2010 at 1:30 am

    Anyone know how to do things like this?

    Reply

Leave a Reply

Your email address will not be published.