CHAPTER 1

image

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 Studio.1  This IDE is also available for free as a light version called Visual Studio Express, which can be downloaded from Microsoft’s website.2

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# 5.0 is the current version which was released in 2012. Each version of the language corresponds to a version of Visual Studio, so in order to use the features of C# 5.0 you need Visual Studio 2012 or Visual Studio Express 2012.

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. To display the New Project window go to File arrow New arrow Project in Visual Studio, or File arrow New Project in Visual Studio Express. From there select the Visual C# template type 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. When you are done click OK and the project wizard will create your project.

You have now created a C# project. In the Solution Explorer pane (View arrow 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.

1 http://www.microsoft.com/visualstudio

2 http://www.microsoft.com/express

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.183.152