C# FAQ 2.3 - How do I build a C# application using Command Line Compilers?

As in other programming languages, you can use command line compilers for developing C# applications. Microsoft's C# Compiler is one of most popular ones available today. It comes with Microsoft's .NET Software Development Kit (SDK). You also can use the compiler that ships with the Mono C# kit. In this FAQ, you will learn how to build a simple C# program using these two different compilers.

This FAQ assumes that you are using Notepad as the editor for the creation of source codes. Open the editor and enter the code as given in listing 2.3.1

Listing 2.3.1

001: // HelloWorld.cs
002: // -------------
003: using System;
004: class HelloWorld
005: {
006:    public static void Main()
007:    {
008:       Console.WriteLine("Welcome to C#");
009:    }
010: }
Note: The line numbers are given only for the sake of explanation and do not form part of the source code.

In Listing 2.3.1, Line 3 defines the System namespace. 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 "Welcome to C#" as output.

Save the file as HelloWorld.cs and compile the code using a C# compiler. I assume you are using the compiler that ships with Microsoft's .NET SDK. For this purpose, you have to give the following command at the DOS prompt:

csc HelloWorld.cs
Note: If you have installed Visual Studio .NET 2002 or 2003, you can compile a C# program by using the Visual Studio .NET Command Prompt. You can launch it from Start | All Programs | Microsoft Visual Studio .NET 2003 | Visual Studio .NET Tools.

If you have installed the Mono C# compiler, you should compile the above program by 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. Additionally, 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 Figure 2.3.1.

Figure 2.3.1: Compiling HelloWorld.cs

To view the output of the above program, you have to supply the name of the assembly (HelloWorld.exe) at the DOS prompt. For instance, you have to give the following command for executing the above program:

Note: An Assembly is a file that is created by the compiler upon successful compilation of every C# application. For more information, refer to FAQ 1.5.

The output will be as shown in Figure 2.3.2

Figure 2.3.2: Running a C# Program

The execution statement for the mono compiler will be as shown below:

mono HelloWorld.exe

Commenting the Code

If you look at line numbers 1 and 2 in the above code, you see two slash lines at the beginning. In programming terminology, these lines are called comments. The 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 because it will help you 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 have worked with C++ and Java. They are single-line and multiline comments.

Whereas single-line comments are given with the // symbol, multiline comments are applied with /*...*/ symbols and can spread across more than one line (see Listing 2.3.2).

Listing 2.3.2

// this is a single line comment
/* This is a Multiline Comment */

The third type of comment, which is given with the /// symbol, is a new one introduced by the .NET Framework. It is used to generate documentation for your C# program and is denoted by using the relevant XML tags as shown in Listing 2.3.3:

Listing 2.3.3

/// <summary>
/// this is a new comment system
/// </summary>

You will learn more about the above comment system in a later FAQ.

About the Author

Anand Narayanaswamy

Anand Narayanaswamy (Microsoft MVP) is a freelance writer for Developer.com and Codeguru.com. He works as an independent consultant and runs NetAns Technologies (http://www.netans.com)which provides affordable web hosting services. He is the author of Community Server Quickly (http://www.packtpub.com/community-server/book). Anand also runs LearnXpress.com (http://www.learnXpress.com) and Dotnetalbum.com (http://www.dotnetalbum.com) and regularly contributes product and book reviews for various websites. He can be reached at ananddotnet@yahoo.co.in


  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Companies undertaking an IT project need to find the right balance between cost and functionality. It's important to start by determining whether to build a solution from scratch, buy an out-of-the-box solution, or a combination of both. In reality, most projects will require some system tailoring to meet business requirements. Decision-makers must understand how much software development is enough and craft a detailed implementation plan to ensure the project's success. This white paper examines the different …

  • Intelligent N+X Redundancy, Placement Affinities, & Future Proofing in the Virtualized Data Center Virtualization brought about the ability to simplify business continuity management in IT. Workload portability and data replication capabilities mean that physical infrastructure failures no longer need impact application services, and they can rapidly be recovered even in the event of complete site failure. However, Enterprises and Service Providers face new challenges ensuring they have enough compute …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date