C# Programming Tutorials for Beginners: Structure


In this C# beginner’s programming tutorial, we will take a look at the basic structure of a C# program. 

We will use the classic programming example of the basic “Hello World” program.  When learning any new programming language this is the example you should start off with.  It shows you very quickly how the new language is structured and you can compare it very easily to another programming language that you might know. 

If you are an absolute beginner to programming, this will give you a place to start.

Once that is out of the way we can move on to more interesting examples and applications.

Hello World Programming Tutorial:

Open Visual C# Express Edition. Select File at the top left and then select “New Project”.  Click on console application and enter a name for your program in the name box at the bottom of the screen.  I called mine “HelloWorld” (exciting I know :-)).


tutorial start


Visual C# should open in the main screen called “Program.cs”.  It should look something like this:




Add the following C# source code to your static void Main() method – in between the curly brackets { }. 

C# Source Code:
Console.WriteLine(“Hello World”);
Console.ReadLine();

Congratulations! You have built your first C# program.  Believe it or not that small bit of code on your Visual C# Express screen is an actual program.
Now click the green arrow at the top of the Visual C# screen and your code will compile and run.  You should have a screen that looks like the following console window:




But what does it all mean?  Let’s analyse it in detail.

Structure of a C# Program:
System:

using System;
The using keywords at the top of the screen enable you to use resources available in the .NET framework (classes and methods already built).  This enables you to re-use code and you don’t have to reinvent the wheel as it were.  The System namespace is a namespace that provides access to all the functionality of the System on which the application sits.

Namespace: 

namespace HelloWorld
This creates your own namespace called HelloWorld  in which your class will sit.

Classes:

 
Classes are blueprints that are used to build objects in C# (and in many other object oriented programming languages).  The class name here is Program.  Classes consist of many different statements and methods one of which must be the Main() method.

The static void Main() method here is where the compiler will start to run the program.

Methods:

 
WriteLine and ReadLine are methods (actions) of the Console class which falls under the System namespace mentioned earlier.  Methods are actions that a class can take to do something.  Round brackets “()” are used to tell the compiler to do something with the contents of the round bracket.  In our example the compiler is told to Write a Line using the text “Hello World”.  (ie: WriteLine(“Hello World”);)  Methods do not always have to have something (called an argument) in the round brackets.  Calling the ReadLine method here is just a hack to get the console window to stay open in order for us to view the program.  If you remove it, the window will shut almost as soon as it opens.

Brackets:

 
So what are the curly brackets for?  Well these brackets are really to demarcate what portion of code belongs where.  The opening bracket “{“ after the namespace HelloWorld tells us that everything that follows, falls within this namespace.  It must have a matching closing bracket “}” which is found at the end of the program.  Visual C# helps us here by highlighting both opening and closing brackets if you click just next one of them.  This can help to avoid errors caused by leaving out the closing bracket.  This is one of the most common problems with beginner code.

Adding comments to your code:

 
You may at some stage (actually quite often) want to add small comments to yourself for later use, or for another programmer to read when reading through your code.  If you want to leave such comments in your code you can use “//” before your comment.  This tells the compiler to ignore anything after the two forward slashes on that line.  It is only for use on a per line basis.  Another option for multi line comments is to use “/*” type out your full comment and then add “*/” to close the comment.

That’s all for now, I’ll leave you with the full source code for this programming tutorial so you can check it against your code.


C# Source Code Example: 

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");

            Console.ReadLine();
        }
    }
}