Lesson 1: your first ‘hello world’ program

The first program that students traditionally write when they learn a new language is a program that simply writes a message to the computer screen which reads “Hello, world!” You can create a ‘Hello World’ program by following these instructions:

Open Visual C# Express Edition – on the Start menu, point to All Programs, and then click Microsoft Visual C# 2005 Express Edition.

On the File menu, click New Project. In the Visual Studio installed templates list, click Console Application, enter MyFirstApplication in the Name box, and then click OK.

Visual C# will create an outline program for you, and display the code for that program in the code window.

Insert the following line of code (bold below) into the outline program in the code window:
Using System;
Using System. Collections. Generic;
Using System. Text;

Namespace MyFirstApplication
{
class Program
{
static void Main(string[]

args)
{
Console. WriteLine(“Hello, world!”);
}
}

That’s it! You’ve just written your first program in C#. Let’s run the program to see what happens.
Running the Program

On the Debug menu, click Start Without Debugging (or just press Ctrl-F5). A command prompt window appears that looks like this:

Press a key to dismiss the window.

The program you just wrote might not seem too exciting, but quite a few things happened when you pressed Ctrl-F5.

Visual Studio detected that it needed to ‘build’ the program, because this is the first time the program has been run.

To build the program, Visual Studio invoked a special kind of program called a compiler, which understands the C# language and knows how to convert it into machine code.

The compiler converted the ‘source code’ for your program (which is just a plain text file containing the C# code you wrote) into machine code which the computer can understand.

Once the compiler finished, Visual Studio took the compiler’s output – which is your program – and ran that program. Because your program is a console application, Visual Studio actually launched a console window and ran the program inside that window.

Once your program had written “Hello, World!” on the screen and finished, Visual Studio displayed the “Press any key” message so that you could see what your program had displayed before the window closed.

You could do all these steps yourself, of course, but it’s much easier to use the Visual Studio Integrated Development Environment (IDE) to do them on your behalf. However, it’s important that you understand what’s going on behind the scenes.
A Closer Look at the Program Code

Now let’s examine the code and see how it works. Although you only added a single line of code to the outline program that Visual Studio generated, the whole program code is important so that the compiler can make sense of that single line.

The code begins with a few lines that introduce the parts of the. NET framework that you will be using in the program. We’ll see how these work shortly.

Using System;
Using System. Collections. Generic;
Using System. Text;

The next few lines specify that you are creating a class called Program inside a namespace called MyFirstApplication.

A class is a container for your code. You must have at least one class in a program, but most programs have several. Classes are a fundamental part of programming in C#, so we’ll look at them in more detail later on.

A namespace is a way of grouping several classes together.



Lesson 1: your first ‘hello world’ program