Lesson 1: Hello World

Welcome to the first lesson in our adventure in C# (C Sharp).

We will be creating a traditional first time  program, called the hello world program. This program will allow to explore C# and Visual studio, and is one of the simplest programs.

First we need to setup Visual studio to create our program. The type of program we are making today is a Console Application, which is a application that doesn’t have a GUI and is only a console (Command Prompt/CMD thing).

Under start hit the New Project link

l1i1

A window should pop up. Click on Templates from the list on the left. Under Templates, click on Visual C#. You’ll then see Console Application appear in the middle. Name your project accordingly, and save it somewhere safe. You will be storing your other projects in the same place. Press OK.l1i2

Your screen should now look something like the picture below. There should be some code already in place. If you notice on the right hand side, there is a solution explorer.  This is like a file explorer for your project. This is the area where you would find all your code, libraries, and related files.

Properties are the setting for your project, where stuff like versions, author, etc. is located at.

References are the files and pieces of code that your code is referencing. This is usually a library or code that someone else wrote, and in this case, it is code that is built into C#.

Program.cs is the file you are programing your  code.

On the left hand side is the main window. This is where you will be programming your code.

l1i3

Now that we are setup, we are going to look at our code. The using  is equivalent to import in Java. It tells the program to import libraries of code during, and is using the code further on in the program.

Namespace is like the package in Java. A namespace is what it infers, a space where names can be in. In programming you cannot reuse names in a program, but namespaces only make it so you cannot reuse names inside the namespace, allowing the same name used in multiple locations.

Class is where you define you define your object, giving it a name. If you can recall, a class/object is like a car, it can have different properties (Variables like colour, trim, etc) and different functions/methods (like drive forward, backwards, etc).

The following piece of code is the the main method. The static means that the function can be run without context, or in a new object. The void means that the function does not return, or send anything back, to the code that requested it. The Main is the name, and the string[] args is the command line arguments that can be given to the program at startup.

static void Main(string[] args)

Now, we will write our first line of code. Here it is:

Console.WriteLine("Hello World");

When you are typing this you may notice a box popping up. This is intelleSense. It auto completes your code, and gives you tips and information.

l1i4

Now hit the start button up top. Notice how there is a debug drop down menu. You can change this to production release, if you are using the advanced debug features in the future.

When you run it, you may noticed that the program closes right away. This is normal, because the code has reach the end, so it closes itself. However, this means you can’t see what is going on. So we insert the following code:

Console.WriteLine("Press any key to continue...");
Console.ReadKey();

This writes the Press any key to continue… to the console. The ReadKey method then waits for a key to be pressed. Since there is no code left, it exits the program.

Congratulations, you finished your first C# program.

Also, if you need help with code, C# is a microsoft created programming language. That means they have one of the best reference resources out there, and examples for almost everything. Check it out: http://msdn.microsoft.com/en-us/library/kx37x362.aspx

GG WP. NO RE. GIT GUD SKRUB.

The whole code is below and you can download it here.

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

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World");
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *