tirsdag den 15. december 2009

Hello C# (CSharp)

I know there are a lot of good Hello Worlds to be written in script language but lets go to something a bit more interesting. Real programming - well the beginning of it.

Visual Studio is an expensive way to be a hobbyist programmer, so Microsoft has made a free version of the big studio - Express. First - download and install it from Microsofts website. (I will not write the link, its better to search for visual studio express)

Now when you have installed C# you can open the studio and create a new project.

You can get a lot of help from the templates, but for this blog I'll just create an empty project to give more understanding of what is happening.
Type in HelloWorld and press ok.

When the project is ready, go ahead and save it so we can see the files. So afterwards you can open explorer and goto the path of the project. Open the files in notepad to see what has happened. You'll find that most of the files are pretty readable and understandable.

Now go back to Visual Studio and see in the solution explorer at the right. Here you can see the project which still is quite empty.

Let's get started
Rightclick the references folder in visual studio and choose "Add Reference". In the dialog scroll down to System.Windows.Forms. Select it and press ok. Now you can see that you have a reference in the References folder.
Right click the project and choose "Add|New item".


Select Code File and lets call it Program.cs (cs is for CSharp) and click Add. Now you have a very empty file. Add these lines to the file.

namespace HelloWorld

{
    public static class HelloWorld
    {
        public static void Main()
        {
            System.Windows.Forms.MessageBox.Show("Hello world");
        }
    }
}
 
If you press F5 to run the program a command line box enters and a dialog which says: "Hello world". Wow.