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:

HelloWorld
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

Comments

  • 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

  • IBM Worklight is a mobile application development platform that lets you extend your business to mobile devices. It is designed to provide an open, comprehensive platform to build, run and manage HTML5, hybrid and native mobile apps.

  • Live Event Date: November 13, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT APIs can be a great source of competitive advantage. The practice of exposing backend services as APIs has become pervasive, however their use varies widely across companies and industries. Some companies leverage APIs to create internal, operational and development efficiencies, while others use them to drive ancillary revenue channels. Many companies successfully support both public and private programs from the same API by varying levels …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds