Monday, March 28, 2016

Your First Program

In this tutorial you will be using the programming language C# (pronounced C sharp), the .NET (pronounced dot net) framework and an IDE called Visual Studio Community 2015 which is free. Simply google for it, download and install it. I have chosen the dark color theme because it is easier for the eyes and I have changed a few other colors.

Once installed and launched we will begin by creating a new solution, which is merely a container for one or more projects.

Step 0 - Select File -> New -> Project...



Step 1 - Select Templates -> Visual C# -> Windows -> 'Console Application' and name the solution 'HelloWorldApplication'.




As you can see below on the left side you should now have a solution called 'HelloWorldApplication' which contains a project also called 'HelloWorldApplication'. A solution and project is simply two different levels of abstractions. On the right side you can see a auto generated snippet of code which I will explain step by step.



At the top (row 1 to 5) you can see a few statements starting with 'using', these indicate that this file has access to certain functionality. A statement is the same as an action that the compilation will translate into machine code.

Then at row 7 you can see 'namespace HelloWorldApplication' which is a logical container for the 'class Program' declared at row 9 to 14. In C# curly brackets (or braces) '{}' are to indicate the limitation of a certain container. The character '{' is referred to as the opening bracket and '}' the closing bracket; there is must always be the same number of opening and closing brackets or the program will not compile. The reason you have namespaces is so that if someone creates a class of the same name there will not be a conflict between them.

To explain the class declaration... In the real world, you'll often find many individual objects all of the same kind. A bicycle company creates thousands of bikes each identical to each other, but still unique. Each bicycle was built from the same blueprint and therefore contains the same components. In object-oriented terms, we say that a bicycle is an instance object (or instance for short) of the class Bicycle. A class is the blueprint from which individual objects are created.

In our case, it is a very simple class called Program which contains a simple method called 'Main'. This method is also static which means that it can be called for without creating an object of the class it belongs to. The 'void' term is an indication of the return type, which means that when the method has finished executing its statements it is will not return any value. The '(string[] args)' part indicates the input parameter to this method; the parameter is called 'args' and 'string[]' is the type this parameter has. Each program must have a method of this kind 'static void Main(string[] args)', this is where the program starts to execute. A 'string' is just plain text and the '[]' square brackets indicate that it is an array of strings, which makes string[] a 'list' of 'texts'. This args variable could for example be used to initiate an application with different settings.

By tradition we will add one simple line of code like below, which makes this a "Hello World" application.



The semicolon at the end is required because it marks the end of that statement. You could put several statements at the same row if you really wanted to as long as you end each one with a semicolon, but it would make it more difficult to read. As we will see in future blog posts there are multiple ways of writing the same code.

If this is your first time programming I would imagine you might feel a bit overwhelmed with all the details, do not worry about not understanding everything completely, it will become clear as you continue. Also, there is no shame in trying out the same things multiple times.

When you run the program using this button , you will probably see a fast flash of a console application running and then disappearing. This is because the program starts running, prints "Hello World!" in the console (cmd.exe) and finishes, which means the console application closes. It is all it does and nothing more, there is nothing that would keep the program running.

To actually see the text in the console, one way is to use a 'break point' which is important feature of the 'debugger'. You can see a break point to the left below. You add one simply by left clicking in that area.



Now, simply run the program again. The program should pause running at the break point.



You should have a console application running on your task bar like so:


You can now 'Continue' or 'Stop' the application. 
'Continue' makes the program continue running as if you never paused it with the break point while 'Stop' makes it quit execution immediately.

Congratulations! You have just created your first program!

What is the next blog post going to be about?

Object Oriented Programming (OOP)

/*
  Good ideas don't require force.
*/

1 comment:

  1. Software Development in Dubai
    https://www.nsreem.com/ourservices/software-development/
    NSREEM develop amazing desktop and web applications that are tailored to your specific requirements.
    NSREEM is #1 in Software Development in Dubai
    1633339546135-9

    ReplyDelete