Labels

Friday, September 26, 2014

Types of C# Statements

There are three types of statements that can be written in a c# application.

Types of statements


1.Sequential statements : that statement will run when comes its turn in the sequence of execution under no condition and it will run one time only.example on the sequential statement is the statement that we wrote in the example 'Create your first C# Application'   which appear in the picture below: 



 2.Conditional Statements (Control Statements): that type of statements will run under one or more conditions that given by us ,so that statements may not run if our condition doesn't pass thetest .
There is two types of Conditional Statements 'if Statement'  and 'switch Statement' and those will be discussed later in details.

3 .Loop Statements (Iteration Statements):loop statements is the statements that executes more than one time according to specific number of loops or under one or more condition.
C# has four types of loop statements as follow ('for Statement,foreach Statement,while Statement,do while statement') ,all these statements will be discussed in details .    

C# data types and variables

What is a Data type?

In each application we create we will use data such as numbers or text .
In the C# Application you have to determine the type of each piece of data you dealing with and that makes the application know what can you do with the data or what you can not do with it .
for example if you have the number 5 you must tell the program what kind of 5 that you want to deal with ,you must tell the application if it is an integer like a number of students for example or it just a number of a street in an address text ,so the application determine the types of operations that can be done on it and the others that can't be done ,so if the number 5 means an integer that means you can add it to another integer or subtract it from another or multiply it by another or divide it by another, but if you mean the number 5 as a text that means that you can't do any of the previous operation on it.

Beside the types of operations that the program can determine from the type of data also the program determine the amount of space needed in memory to store the data based on its type ,meaning the space needed to store the number 5 as integer in 32 bytes but space needed to store the number 5 as text is more than that ,so the right type makes the application work write.

Most Commonly used Data types in C#

int  ( integer ) used for numbers without floating point.
doubleused for numbers with floating point.
string : used for text.
char:one character.
bool:used to store one value of two ( true , false ) .

Variables

What is a Variable?

A Variable is space in memory (RAM) that can we use to store a piece of data ,we give that space a name so it can be easily reached any time later .


How to Declare a Variable in C#

Declaring a Variable is so easy ,you just determine the data type of the data that will be store int the variable than give the variable a name ,see the picture bellow:

int number;

As you can see in the picture above we declared an integer variable using the keyword 'intwhich refer to the integer data type ,after the type we have a space and then the name of the variable which we can freely choose it  then comes the semicolon ';' to tell the application that the statement ends here.

Notice that under the Variable name there are a green line that tells there is something wrong ,do not worry ,the visual studio tells you that the Variable that you declared has no data yet.

Assign a value to the variable 

Once you declared the Variable you can assign a  value to it by calling its name followed by equal sign ' = ' followed by the value that you want to store ,see the picture bellow : 

number = 5;

You also can Declare the Variable and assign a value to it in one step ,see the picture below:

int number = 5 ;

Now we can make some Variables of different data types ,see the picture below:

The previous picture shows the different types of data types and how each type take its value .
Notice the 'string' variable takes the value surrounded by double quotes and the 'char' variable surrounded by single quotes.

Variable Naming rules

When you declare a Variable you must consider the following :
  • Variable name must not be a C# reserved keyword :the C# language has reserved keywords that can not be used as variable name ,these keywords usually takes the blue color like 'int,double,string,char,bool,namespace,class,static,void,publicetc,follow this link to find  a complete list of C# reserved keywords .
  • You can not use spaces in the Variable name ,meaning if you want to give a name to a Variable that contain more than one word do not use spaces but you can use underscore instead,or you can give a name to your Variable using 'CamelCase' technique ,meaning that you can write the variable name without spaces starting each word with upper case letter such as 'MyFirstLetter' , follow the following link to read more about Camel Case .
  • Variable name can not start with a number ,you can use numbers any where in the Variable name but not the beginning of the name, the Variable name must be a letter or Underscore .


  

Advertising

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

Build Your first C# Program

After Visual Studio installation, you will be ready to create your first C# Application,and to do that follow my steps.

1-from your start menu click on 'All Programs' and under the Visual Studio folder click on the Visual Studio icon ,see the picture below.

2-after opening the Visual Studio go to the 'File' menu and hover over 'New' then click on 'Project',see the picture below.


3-A window like that shown in the picture below will appear from this window make sure that you select 'Visual C#' from the list on your left then from the list in the center select 'Console Application' ,then below this list change the 'Name' to 'HelloWorld' and press 'OK' ,see the picture below.


A new C# application is now created ,and you should have a screen like this ,see the picture below.


Congratulations ,you have Successfully create a C# Program,now you can run the application using Ctrl+f5 or from the 'DEBUG' menu click on 'Start without Debugging' .
You should get a screen like this,see the picture below.


This blank black screen called a Console Application ,this type of application is used mainly for training and studding the language and it also used for creating a real world application used for specific purposes that we will not talk about it on the blog.

Is that all?

.Now you may ask your self 'is that all?' ,my answer is no ,we just crated a C# Console Application that do nothing ,so let's give the application something to do.

Write your first line of Code.

Now return to the Visual Studio and write this line of code exactly as shown in the next picture:


This line 'Console.WriteLine("Hello World , I AM Developer ");' tells the console application to write some text on the console screen .

Now run the Application again using 'Ctrl+f5' ,you should get the following result as shown in the picture below:


What does this line of code mean?

Lets describe this line 'Console.WriteLine("Hello World , I AM Developer ");'

 The word 'Console' refers to a Dot NET class that contains a lot  of methods (procedures)  that are ready to be used by developers like 'WriteLine' the one we used in this example which tells the Console Application to a line of text on the screen , after the word  'WriteLine' comes two brackets which contains the text that you want to write on the screen surrounded by double quotation ,at the end of statement we notice that there are semicolon ';' , the semicolon tells the application that the statement ends here so the application goes to the next statement.

Whats  Next?

Now we can create a C# application and write some code on it but we still need to learn more about the language .
In the next lessons we will start to learn the language bit by bit ,so go to the next page.

Advertising

What is the tools that i need to learn c#?

What is the tools and the software that we need to start ?

this is an important question and before we start we have to answer it.

The most important software you will need is an IDE (Integrated Development Environment) ,
and there are many, but in this blog we will use Microsoft Visual Studio because it's powerful and very easy to use .
Microsoft offers many versions of Visual Studio that you can purchase from Microsoft and there are also a free version of Visual Studio called Express Edition and you can download it for free from yhis link : Download Visual Studio Express

After Downloading Visual Studio you have to install it then you are ready to start.

Saturday, September 20, 2014

About the blog

This blog will focus on teaching the C# basics for beginners and will start from the language basics to creating your first application,
so if you willing to learn C# language just follow us.