BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

C# Programming
.NET 1.1+

Hello World

This is the first in a series of articles exploring the fundamentals of the C# programming language. This first part of the series describes the creation of a simple C# program that outputs the phrase "Hello World".

Tutorial Prerequisites

The .NET Framework includes all of the software required to compile a program that can be developed using any standard text editor. However, Microsoft provides many development environments that are suitable and make programming and debugging much easier.

Microsoft Visual Studio is an ideal development environment for this tutorial if you have it available. If not, I strongly suggest downloading Microsoft's excellent, and free-of-charge, development environment, Microsoft C# Express Edition

Hello World History

In 1978, Brian Kernighan and Dennis Ritchie published the book, "The C Programming Language". This included a very simple program that demonstrated the C language by outputting the phrase, "hello, world". The program showed the structure of a C program and allowed the programmer to try out their compiler. This article will describe the equivalent C# program.

Console vs Windows vs Web Application

The .NET framework, combined with C#, allows you to develop several types of software. These include Windows-based programs, web-based applications and even console applications for execution from the command prompt. In this article, we will create a console application similar to the original Hello World program. A console application requires little supporting code and gives the clearest opportunity for understanding the language.

Creating the Program

Step 1 - Create a Console Application

Console Application IconI will assume that you are using Microsoft Visual Studio or Microsoft C# Express Edition to develop the program. Launch your chosen development environment and create a new project of the type, "Console Application". Name the application, "HelloWorld".

Step 2 - Add the Functionality

The development environment should have created all of the template code needed for a console application. This includes a "Main" function that is similar though not identical to the original K&R application. All that is left is for you to do is to add the code that actually prints "hello, world".

C# is derived from C but does not share all of its commands. We cannot use the printf command to output text, as this command is not supported. Instead we use the Console.WriteLine function. Add the following code between the braces {} of the Main function, ending the second line with a semicolon (;).

// Output the greeting text
Console.WriteLine("Hello world");

Your entire source code should now look similar to the following:

using System;
using System.Collections.Generic;
using System.Text;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            // Output the greeting text
            Console.WriteLine("Hello world");
        }
    }
}

Step 3 - Test the Program

You can execute your program directly from Visual Studio or C# Express Edition by selecting "Start Without Debugging" from the Debug menu or by hitting Ctrl-F5. You should see the words, "Hello World" in a console window and an instruction to press a key to continue.

NB: You would usually start programs with debugging enabled by pressing F5. In the case of a console application, the application may close before the results are seen. Using Ctrl-F5 adds the requirement to press a key when the application stops.

23 July 2006