Alright, lets get started!
First thing we’ll need is the Visual Studio 2010 Express Edition provided by Microsoft. Don’t worry, you can get the Express Edition for free. The setup is fairly straight forward but may take a few minutes to complete.
Once this is installed, launch the application. If you haven’t run the application before, you might be prompted to configure your workspace environment. You will be given a few options, feel free to choose whichever option feels the most comfortable.
After the workspace has been configured you will see your start page. The start page can be disabled through the tools menu, but for these tutorials we’ll leave it on. On the top left side of the Start Page you will see a “New Project” button, click it. You will be prompted with a new project wizard. Make sure you have C# selected on the left column, and the “Console Application” type selected in the main column. Name the project “Hello_World”. Where you choose to save these tutorials is up to you, as with all things USE MEANINGFUL NAMES and no spaces, underscores are preferred over spaces.
When you click okay the editor will be displayed and you will see.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hello_World
{
class Program
{
static void Main(string[] args)
{
}
}
}
Now, you’re probably thinking “holy crap, what does it all mean”, don’t worry, we’ll work through this. As of right now, this program is pretty much completely useless, so lets add some functionality to it and get things rockin. We will be editing the “Main function during this tutorial, don’t worry about the details of functions just yet, that will come in a later tutorial. The Main Function looks like this:
static void Main(string[] args)
{
}
Now, we’ll write our own program!
Between the curly-braces”{ }” of the function add
Console.WriteLine("Hello World");
remember to end the line with a semi colon “;”
your entire program should look like this when you’re done:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hello_World
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World");
}
}
}
When you were typing you probably witnessed a little window popping up. This is the Intellisense technology that was bundled with Visual Studio. You will learn to love Intellisense! We’ll go into that more in a later tutorial.
This program can now be built and tested. To test your program, press ctrl-f5, or click debug>start without debugging. If everything worked out properly you should be prompted with a command prompt window displaying the text “Hello World”.
Alright! now that the main program has been created, lets discuss all the little bits and pieces from the top down.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
these four lines are the Namespaces that are utilized by the program. A Namespace is essentially a grouping of common functionality. We’ll dive deeper into these in later tutorials. The next section of code is:
namespace Hello_World
{
somestuff
}
this is the namespace that our program is contained within. The { and } brackets are used to bunch logical blocks of code together, curly braces and semi-colons are a major part of the C# programming language.
Next up is the class keyword
class Program
{
somestuff
}
a class is an essential mainstay of object oriented programming(OOP), the functionality of a program is contained within one or more classes. We will explore classes in detail later on.
static void Main(string[] args)
{
Console.WriteLine("Hello World");
}
The last section of code we will discuss is the function, or “method” is C# terms. The Main function is the first method called when the program is run.
The static keyword attached to the function allows for the function to be called without an instance of the class being created, this may sound complicated right now, but we’ll discuss it in detail later.
The void keyword attached to the function indicated that the function does not return a specific value type, eg. int, string, float, double, etc… “Main” is the name of the function, attached to it are parameters that can be passed to the method, these are surrounded by parenthesis ( ). We won’t really discuss the details of the parameters right now, as this is just an simple introduction to the language.
The final bit to discuss is
Console.WriteLine("Hello World");
This is the line responsible for the output on screen. WriteLine is a method contained within the Console object. You are passing a string literal parameter for the method to display. As shown before, the semi-colon marks the end of a statement.
That about it for the Hello World tutorial. Experiment using the WriteLine method until you are comfortable then move onto the next tutorial when you are ready. We will be discussing the concept of variables and comments next.