C# Tutorial - Application 1 – Hello World


Most programming tutorials start with a simple program that displays "Hello world" on the screen. This has a history that stems back to the good old days of DOS, before the event of windows. Every application ran from the command line, so before we move on, just to make sure that you understand what the command line is, we will open it from within Windows.

Click on the start menu and you should see an entry marked Run... . When you have done this, a dialog box will appear. In the text type cmd and click on OK. A dialog box will appear.

To close the dialog, type exit and hit return. This is how console applications look. They execute by either double-clicking on the application icon in windows, or by typing the name of the application from within a console.

Hello World!

If you have not already done so, open C# Express so that we may create our first application. Create a new project by choosing New Project from the File menu, alternatively you can use the keyboard shortcut Ctrl+Shift+N. This will open the New Project dialog.

When you first open the New Project dialog it offers six different project templates.
  • Windows Forms Application
  • Class Library
  • WPF Application
  • WPF Browser Application
  • Console Application
  • Empty Project

We want to create a C# Console Application called App01. So select these options and enter App01 in the Name field. Once this is done, C# Express will display the code for the application in the code editor window. Change the code so that it looks like the following:

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

namespace App01
{
  class Program
  {
    static void Main(string[] args)
    {
      Console.WriteLine("Hello World!"); 
      Console.Read();
    }
  }
}

Running the Application

Before we run the application, we need to compile it. This can be achieved by choosing Build Solution from the Build menu, or by pressing the shortcut key F6. If the application has any errors, they will be displayed in the Output window.

Because we are building a simple application, then we do not expect any errors and we can run the application. To run the application choose Start Debugging from the Debug menu, or use the shortcut key F5, or click on the green arrow in the toolbar. The text "Hello World!" will be displayed in a console window.

Press any key to close the application.


<< Previous Contents Next >>

© Publicjoe, 2008