A Simple C Sharp Console Application

Revision as of 18:33, 10 January 2008 by Neil (Talk | contribs) (Executing a Compiled C# Program)

Revision as of 18:33, 10 January 2008 by Neil (Talk | contribs) (Executing a Compiled C# Program)

In the early 1970's the creators of the C Programming Language wrote a book of the same name intended to teach the skills necessary to program in C. One of the first chapters of this book contained a very simple C program which simply displayed the words "Hello World" in a console. Ever since that day most programming books has followed this tradition. Given that C# can trace its ancestry to the C programming language C# Essentials will be no exception to this rule.

Even if you are an experienced programmer this simple C# example is still recommended if only to verify that the C# development environment is correctly installed.

Even an example this simple requires that certain aspects of the C# and runtime environment be set up. Before we plunge into the example, therefore, we first need to spend a little time talking about setting up the environment.

Options for Installing a C# Environment

There are a number of options for installing an environment suitable for compiling and executing a C# program. If you are running on Windows you can either install Visual Studio (either the professional edition or the free express edition). If you prefer a lighter weight solution you can simply download and install latest .NET Framework which includes the C# compiler and the runtime environment. This can be downloaded from the Microsoft website.

Once installed the compiler will need to be run from a command prompt window. The compiler executable is called CSC.EXE and by default it is not in the command path. Find where CSC.EXE is installed on your system (in .NET Framework 3.5 on Windows XP it is installed in the \WINDOWS\Microsoft.NET\Framework\v3.5) by performing a Search of your disk drives. Once the location has been ascertained, add the location to the path environment variable:

Set PATH=%PATH%;%Windir%\Microsoft.NET\Framework\v3.5

If you are running on a non-Windows system such as Linux you will need to use either Mono or DotGNU. The Mono compiler command is mcs.

C# Source File Naming Conventions

Before we create a simple C# program it is first necessary to talk a little about C# source file naming conventions. Firstly, although the C# compiler will compile any C# source file regardless of filename extension, convention dictates that such files be given a .cs extension to clearly identify the files as C# source files.

Java programmers are no doubt familiar with the need to use as class name as the name of the file. For example, if a Java source file contains the code for a class called MyClass the file must be named MyClass.java. No such restrictions apply to C# source files, although it is good practice to ensure that source files have meaningful names that go at least some of the way towards describing the purpose of the code contained therein.


Creating a Sample C# Program

Once the appropriate environment is installed and configured the next step is to write our Hello World example. We shall name our example file HelloCsharp.cs. Using your favorite code editor create the following C# source code:

class HelloCsharp
{
       static void Main()
       {
            System.Console.WriteLine ("Hello from C#.");
       }
}

Those familiar with C++ or Java will feel extremely comfortable with the above code and will clearly see the C#'s lineage from these languages. Having saved the C# source code the next task is to compile it.

Compiling C# Code From the Command Line

Having written the C# source code the next step is to compile it down to the Common Intermediate Language (CLI) format. If you have installed the .NET Framework and configured your PATH so that the C# compiler can be found then you are ready to compile. If, on the other hand, you installed Visual Studio you can launch a Command Tool ready compile by selecting Start->All Programs->Microsoft Visual Studio->Visual Studio Tools->Visual Studio Command Prompt.

Once a command prompt is displayed, simply enter the following command to compile the sample program:

csc HelloCsharp.cs

The compiler will display output similar to the following and compile the code to create a HelloCsharp.exe executable:

C:\Documents and Settings\Neil>csc HelloCsharp.cs
Microsoft (R) Visual C# 2008 Compiler version 3.5.21022.8
for Microsoft (R) .NET Framework version 3.5
Copyright (C) Microsoft Corporation. All rights reserved.

If the compiler displays any syntax errors go back to the source and double check you haven't made any typing errors. Remember that C# is case sensitive. A common mistake made by C programmers, for example, is to write Main as main which will cause the compiler to display a syntax error.

Assuming there are no syntax errors, the next step is to execute the program.

Executing a Compiled C# Program

Assuming the absence of syntax errors, the C# compiler will have created an executable file called HelloCsharp.exe. Running this program executable is simply a matter of entering the name at the command prompt:

C:\Demo> HelloCsharp
Hello from C#.

If you see the "Hello from C#" message then you have successfully written, compiled and executed your first ever C# console program. In the next chapter we will learn how to create a simple GUI based application using Visual Studio.