Tuesday, March 29, 2011

First C# Program Line by Line

The following is a typical Hello World program for C#.  On the left side is the code and the right side its explanations. 

using System;

This reference is used by Console class. Otherwise, you need to type the class in full (to include its namespace System):

System.Console.WriteLine(“…”);

using System.Collections.Generic;

Not used. Something useful that Microsoft tries to promote.

using System.Linq;

Not used. Something useful that Microsoft tries to promote.

using System.Text;

Not used. This reference contains the StringBuilder class that is memory efficient when working with strings manipulations.

 

Blank line or tab or space has no effect on the final compiled code. Add them to improve the code readability.

namespace ConsoleApplication1

A way to group related code together. By default, Visual Studio will use your application name as the namespace.

{

1st curly bracket START

Curly brackets { and } comes in a pair. They group block of code.

Everything from here till the 1st curly bracket END, belongs to the namespace ConsoleApplication1

class Program

Everything in C# must in a class. In this case, the name of the class is

ConsoleApplication1.Program

(You need to include the namespace as part of the name of the class)

{

2nd curly bracket START

Everything from here till the 2nd curly bracket END, belongs to the class Program

static void Main(string[] args)

static: it is a keyword to specify that the method Main is “static” . It belongs to the class and not instances of the class.

void: it is a keyword to specify that the method Main returns nothing.

string[] args: a string array variable with the name args

curly bracket { } : for code block

curve bracket ( ) : for method

square bracket [ ] : for array type

{

3rd curly bracket START

Everything from here till the 3rd curly bracket END, belongs to the method Main

Console.WriteLine("Hello C Sharp!");

Code doing the actual work!

}

3rd curly bracket END

}

2nd curly bracket END

}

1st curly bracket END

I have started a blog for beginners

0 comments: