Labels

Friday, September 26, 2014

Understand C# program structure

Now we will discuss the minimum basic structure of a C# program and understand each element in the structure.

In the last discussion we learned how to Create a C# Program and we wrote some code on it .

The Application the we created shown in the picture below:


Before we start we should learn some Keywords

  1. A Method: a container of C# code statements ,a Method can have one or more statement on it.
  2. A Class : a container of Methods and Variables each class can contain zero or more members , a member can be a Method or a Variable.
  3. A Namespace : a container of Classes ,think of it as a folder that contains files on it ,kind of organizing related classes . 

What does each line means? 

  • 'using System' this line of code means that we need to include the namespace 'System' into our application and that because we need its component ,and each 'using' statement used to include another name space into our application.
  • 'namespace HelloWorld' this is the name of the application namespace ,every C# application must contain one namespace at least ,and it takes the application name by default ,as you can see it says 'Hello World' .
  • '{ }' Curly braces determine the begin and the end of a block of code ,after the namespace we notice that there is beginning of curly braces indicating the beginning of the namespace block and it's end at the end of the code.
  • 'class Program' this defines a class called 'Program'  ,each C# program must have at least one class, the first class usually takes the name 'Program' and you can change this name if you want.
  • '{ }' Curly braces determine the begin and the end of a block of code ,after the class definition we notice that there is beginning of curly braces indicating the beginning of the class block and it's end before the end of the namespace .
  • 'static void Main(string[] args)'  that line called a method declaration ,it declares the Main Method of the application ,that method represents the entry point of the application ,when the application start running it begins with the Main method ,the Name 'Main' must not be modified and this method must not be deleted or replaced if so the application will fire an Error says that the application can not find a start point.
  • '{ }' Curly braces determine the begin and the end of a block of code ,after the Main Method definition we notice that there is beginning of curly braces indicating the beginning of the Main Method block and it's end before the end of the class.

Advertising

No comments:

Post a Comment