News & UpdatesProgrammingWeb programmingPartnersStore
Book Cover
Buy Now
Projects
Links

C# Tutorial – 02 – Hello World

Choosing an IDE

To begin coding in C# you need an Integrated Development Environment (IDE) that supports the Microsoft .NET Framework. The most popular choice is Microsoft’s own Visual StudioVisual Studio. This IDE is also available for free in several light versions that can be downloaded from Microsoft’s website. The one used for programming in C# is Visual C# Express.

The C# language has undergone a number of updates since the initial release of C# 1.0 in 2002. At the time of writing, C# 4.0 is the current version which was released in 2010. Each version of the language corresponds to a version of Visual Studio, so in order to use the features of C# 4.0 you need Visual Studio 2010 or Visual C# 2010 Express.

Creating a project

After installing the IDE, go ahead and launch it. You then need to create a new project, which will manage the C# source files and other resources.

  • If you have Visual Studio (2010) go to File→New→Project to display the New Project window. From there select the Visual C# project types in the left frame. Then select the Console Application template in the right frame. At the bottom of the window you can configure the name and location of the project if you want to. Click OK and the project wizard will create your project.
  • If you use Visual C# Express (2010) go to File→New Project. In the New Project window select the Console Application template and change the name your project if you want to. Next, hit OK to generate the project.

You have now created a C# project. In the Solution Explorer pane (View→Solution Explorer or View→Other Windows→Solution Explorer) you can see that the project consists of a single C# source file (.cs) that should already be opened. If not, you can double-click on the file in the Solution Explorer in order to open it. In the source file there is some basic code to help you get started. However, to keep things simple at this stage go ahead and simplify the code into this.

class MyApp
{
  static void Main()
  {
 
  }
}

The application now consists of a class called MyApp containing an empty Main method, both delimited by curly brackets. The Main method is the entry point of the program and must have this format. The casing is also important since C# is case-sensitive. The curly brackets delimit what belongs to a code entity, such as a class or method, and they must be included. The brackets, along with their content, is referred to as a code block, or just a block.

Hello World

As is common when learning a new programming language the first program to write is one that displays a “Hello World” text string. This is accomplished by adding the following line of code between the curly brackets of the Main method.

System.Console.WriteLine("Hello World");

This line of code uses the WriteLine method which accepts a single string parameter delimited by double quotes. The method is located inside the Console class, which belongs to the System namespace. Note that the dot operator (.) is used to access members of both namespaces and classes. The statement must end with a semicolon, as must all statements in C#. Your code should now look like this.

class MyApp
{
  static void Main()
  {
    System.Console.WriteLine("Hello World");
  }
}

IntelliSense

When writing code in Visual Studio a window called IntelliSense will pop-up wherever there are multiple predetermined alternatives from which to choose. This window is incredibly useful and can be brought up manually by pressing Ctrl + Space. It gives you quick access to any code entities you are able to use within your program, including the classes and methods of the .NET Framework along with their descriptions. This is a very powerful feature that you should learn to make good use of.

If you like this tutorial please +1 it:


2 Responses

September 8th, 2011 at 06:33  

nice tutorial video.

September 8th, 2011 at 01:17  

how can i download this video?

Leave a Reply