Friday, February 20, 2009

Simple Overview

Today i will start on the basic syntax of C#.. first we will see a typical hello world application in C# and i'll try explaining them step by step.

First you have to create a new console application.. The reason why we start with console applications is mainly because you get to familiarise with the syntax (rules and principles of a language that you must follow) before you continue to windows applications.. i know its boring but please bear with it!.

First you open Visual C# Express 2008, or Visual Studio 2008.
The difference between the two IDE is mainly the language supported, Visual Studio supports more languages.. and since its a commercial IDE (VCE is free) there are more functionality than the Express versions.. some of which include class diagram, conditional breakpoints etc.

As i am using Visual Studio 2008, there might be some difference but i am sure its almost the same.
  1. Click on File Menu > New > Project.
  2. Make sure you are using Visual C# as the language, Under Visual C# > Windows choose Console Application then give it a name, in my case i named it HelloToMe and press OK. The IDE will generate the required files
  3. Double click the Program.cs file on the Solution Explorer (by default it should be on the top right corner) and enter the highlighted line as follows:

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

namespace HelloToMe
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello To ME!"); //outputs Hello To ME!
}
}
}
After which you can click on the Green button which says Start Debugging which will result on the following output on the console window.
Hello To ME!

Congrats~ Now you have made your first application (hopefully and maybe not =s). Either way we still have to go through the codes. Don't worry if you don't understand the terms used. Slowly you will...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
The first four lines of codes as seen here is the are called using directives, it is used by the compiler to look for types in used in your classes if you do not provide a fully qualified name for your types used, they are used to save time typing the full name.

using System; //with using System;
Console.WriteLine("Hello");
Console.WriteLine("Hello Again");
Console.WriteLine("Hello Again and Again!");

System.Console.WriteLine("Hello"); //without using System; (fully qualified name)
System.Console.WriteLine("Hello Again");
System.Console.WriteLine("Hello Again and Again!");
Example: Without using directives vs With using directives

Next, we have the namespace declaration in line 6 (This is the namespace that is related to the previous using directives).
Namespaces are used to organise and group types together under a name to avoid crashes among different types with the same name.

It is recommended that name your namespace according to your company name followed by your department name and sub-department so on.. for example (Rejinderi.HumanResources.CoffeeMakers). The levels of the namespace can be specified using the . dot operator.The dot operator has a couple of other usage most importantly as member access operator but we won't talk about those now.

Namespaces can be nested so the following two namespaces are actually the same they can be referred by Rejinderi.HumanResources
namespace Rejinderi.HumanResources
{
class MyClass //fully qualified name: Rejinderi.HumanResources.MyClass
{
}
}

namespace Rejinderi
{
namespace HumanResources
{
class MyClass //fully qualified name: Rejinderi.HumanResources.MyClass
{
}
}
}
Classes and types will be covered later on.
Next we have the Main method

static void Main(string[] args)
{
Console.WriteLine("Hello To ME!"); //outputs Hello To ME!
}
Method is a code block (surrounded by parenthesis {}, so are types and namespaces) that performs a routine.
They may or may not return a value according to the return type in this case the keyword void specifies that the method has no return type.
They can receive information called parameters through a parameter list.

In this case string[] args is the one and only parameter for this method.
Methods will be covered in detail later on, the Main method (as shown above) is a special method as it is the first execution point for all C# applications. It is automatically called when the application executes. You execute a method by calling them.

Lastly for this application we have the Console.WriteLine method call, which you added in yourself, what this method does is prints a line to the console application screen followed by a carriage return (new line), it accepts a variety of overloads(parameter list types, 19 to be exact).
You call a method by specifying the method name (Console.WriteLine) followed by open bracket then the parameter(s) to pass in then the close bracket. Followed by a semi colon.
In this case it would be
  • Method Name: Console.WriteLine
  • Parameter: "Hello To ME!"
Take note that all C# statements end with a semi colon.
Again, please do not worry about the things you don't understand here, they will be covered soon.

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home