C# Tutorial Part 1.0 – Hello World!

Welcome to part 1.0 of my C# tutorial. (Parts 0.1 and 0.2 were ‘getting started’ parts really) We will now look at how to create a simple hello world program.

A Hello World program is traditionally the first program you create when learning a new language. All they actually do is print the words “Hello, World!” (or a slight variation on that) to the screen. Because of this, they are often quite simple, and a great place to start a tutorial. So here we go:

Hello World in C#

We will now run through what each line means.

Let’s start with the simplest lines – the green ones.
They are comments.
They don’t mean anything.
You could leave them out and the program would be unaffected, however it is important that you get into the habit of commenting your code, as it will become increasingly important as your programs get larger and more complicated.
Comments allow you to keep track of what each line/function/block of code does, and also provide help to other people who might be taking over your programming project.
The most simple way to leave a comment in C# is to put a double forward slash, then write whatever you want to after it. You can do this anywhere, even on the end of a line.
Everything after the // to the end of the line will be ignored when the program is being compiled.
// This is a comment

Now the actual programming statements, starting from the top:

using System;
This line tells the program it will be using functions from the System namespace.
Note the semi colon on the end of the line.
Semi colons are probably the most hated symbols in all of programming – you have to put one after every statement, and if you don’t your code gets an error.
It’s quite easy to forget this, and it’s lucky C# tells you what line the error is on or you’d have to go scrutinizing every single line to work out where you should have put a semi colon.
What the semi colon actually does is tells the program where one statement ends and the next begins.

public class helloworld
{
This starts a new class to contain your code.
The code inside the class is contained inside curly braces: { and }.
Code within these braces is called a ‘block’ of code. The } right at the end of the program closes the class.
The word ‘public’ means that other classes can access it and its contents.
This is called an access modifier, and will be covered later in the tutorial

public static void Main()
{
This starts a new method within the class we just created. Methods are what actually contain code, and you can create as many as you like, each with different names, which contain code which does different things. Classes group your methods together.
This method is called Main, which means it is the point the program starts running from.
The word ‘public’ means other methods contained inside other classes can use it.
The word ‘static’ means that other methods cannot make copies (instances) of it. If I left out the word static, it would just become a template, and would not run unless I created an instance of it, but I would have nowhere to put the code to create the instance, so the program would have an error.
You do not need to understand instances at this point, but you do have to know that there must always be a ‘static’ ‘Main’ entry point to the program, otherwise it will not work.
Methods like main can have return values and parameters.
The word ‘void’ tells the program that the Main method has no return value, so it will not return an ‘answer’ to whatever called it. (Nothing called it in this case because it is the Main method and executes automatically)
Main is the name of the method. It must have a capital M because C# is case sensitive
The brackets arranged like this: (), indicate this Main has no parameters. Brackets are known here as parenthesis.
Parameters are values given to a method, so it can perform it’s task. You could have a calculator program, with a method that adds two numbers together.
The parameters of that method would be the two numbers you want to add
Finally, the code inside the method is contained inside curly braces, explained above. The second last } in the program finishes off the Main method.
All you need to understand from this at the moment is that Main is the entry point of the program, and that it must be static. (You can leave out the word ‘public’)

We then have a comment explaining what the program does

The next line is where the program gets interesting.
This line is inside the Main method inside the class helloworld.
Console.WriteLine(“Hello, World!);
This line tells the program to print the words “Hello, World!” (without the quotes) to the screen.
It does this by calling the method WriteLine from inside the class Console. The class Console is inside the namespace System, which we mentioned in the first line of our code.
This WriteLine method is called with one parameter inside the parenthesis: a string of letters.
So basically:
We’ve already told the program’s compiler to look in the System namespace
We’re telling the program to run the method WriteLine, and telling it that it is inside the Console class. (Using the dot .)
The program will call the method, passing on the string of letters we specified.
The method will get the sting and it can do anything it likes with it, but it has been programmed to print it to the screen.
You could change the string to anything you like, for example, you could make a program which says “I like cheese”. Just remember to put the speech marks (double quotes) around it, because that’s how the program knows it’s a string.
And also, never forget the semi colon! (Explained above)

At this point, we’ve done what we need to do, and we could just finish the program by putting two curly braces } } one to finish the Main method block of code, and one to finish the helloworld class. However, if we do that, when we run the program, it will appear, print Hello World and shut itself down instantly. That’s no use because you need time to marvel at your first C# program. The next line in the program slows it all down and gives you a chance to do that:
Console.ReadLine();
We will look at the ReadLine method in Part 2.0, when we cover input.
All you need to know for now is that this statement tells the program to wait for some input from the user.
It won’t do anything with the input: we only put it in to slow the program down and give you a chance to read “Hello World”
Remember the semi colon.

Then we have another comment

Then the program is finally drawn to a close with two closing curly braces to finish off the method Main and the class helloworld

So that’s it – a more advanced Hello World program in C# explained in too much detail. (There will be plenty of time to come to understand everything later, if you don’t already).

Type this in to a new console application, (see Part 0.2 of this tutorial for help) hit debug and check out Hello World in C#. The code is displayed here as a picture, so you can’t copy and paste. Go carefully and make no mistakes.

I will set some tasks to do in Part 1.1 of the tutorial, coming soon.

Advertisements

About Matt

I like writing, filmmaking, programming and gaming, and prefer creating media to consuming it. On the topic of consumption, I'm also a big fan of eating.
This entry was posted in Programming. Bookmark the permalink.

Enter comment:

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s