Hello World Tutorial

Tutorials

Since this is my first actual post, I thought I should start at the very beginning – with a traditional ‘Hello World’ example.

During these examples I am going to be using Microsoft Visual Studio 2008 to write and compile my C# code. If you do not have Visual Studio installed on your pc, you can download the Microsoft Visual Studio Express Editions from here.

Once installed, open Visual Studio and click on the File menu. Select New > Project and you will see a screen similar to the one below.

Click for full size image.

Click for full size image.

From this screen you can create many different types of new Visual Studio Projects. For this example though, we need to select a ‘Visual C# Windows’ project type from the left window and from the right window we need to select the ‘Console Application’ template. Then enter a project name and for this example we are using ‘HelloWorld’, and finally click on the ‘OK’ button.

You will now end up with some automatically generated code which looks like this:

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

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

Now this code will compile and run but it does absolutely nothing at this point. So let’s add some code to output the text “Hello World from daveoncsharp.com!” to the screen. In the static void Main method add the following line of code:

Console.WriteLine("Hello World from daveoncsharp.com!");

And there we have it – our cool ‘Hello World’ application. You can run the program by clicking on the Debug menu and selecting Start Debugging or alternatively you can hit F5 on the keyboard.

Now if you noticed correctly, our ‘Hello World’ application opened up and closed almost instantly and we didn’t get to see the output text on screen. This is because the application terminates immediately – it does not wait for you to read what’s on screen.

To fix this small problem we can add this code Console.ReadLine(); right beneath the Console.WriteLine we added earlier. Once this is done your final code should look like this:

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

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World from daveoncsharp.com!");
            Console.ReadLine();
        }
    }
}

The Console.ReadLine(); waits for the user to input text and then hit the enter key and that’s why we can see our text on screen.

This tiny application can even be written in less code than is shown above. We can remove the namespace because there is no need for it in this example (I will be discussing namespaces in the future), we can remove the string[] args from the Main() method because we are not reading any command line arguments in this example, and we can also remove some of the using statements at the top because this application does not make use of them either. So our new version of the code would look like this:

using System;

class Program
{
    static void Main()
    {
        Console.WriteLine("Hello World from daveoncsharp.com!");
        Console.ReadLine();
    }
}

Visual Studio adds all that unused code because normally when writing an application you would make use of them so it adds them automatically for you.

We have now successfully created our little ‘Hello World’ application using C#.NET.

I hope this article was of some help to you beginner coders out there. Please feel free to leave your comments below.

Stay tuned to this blog for more articles in the near future.

Dave

0 comments… add one

Leave a Comment