Read chapters 1 to 4 from Microsoft Visual C# 2005.
Here’s a summary of what I read:
Variables:
A variable is a memory location that holds a value. You name it, so its name makes reference to what it holds.
Naming conventions:
· You must not use underscores.
· Don’t difference them by case only. (Variable, variable).
· Use camelCase notation (multiword only, first word start with lower case in the first letter, then for other words use upper case for first letter and continue in lower).
Declaration of variables is done by writing the type of it and its name. i.e.: int number;
Methods:
Methods are "sequences of statements"(Microsoft Visual C# 2005 Step by Step, John Sharp). They have a name (refers to what the method does) and the body (step by step of what the method will do).
Declaring a method:
returntype methodName (parameterList)
{
body statements
}
The return type represents the type of information the method will return (or void if it returns nothing). The parameter list are the different things the method needs to do what it’s supposed to (i.e.: numbers, words, lists, etc.). When you want your method to return information you use the return keyword. The return statement must return the same type specified by the functions return type.
Method calling is done as following: methodName (parameterList);.
Scopes: The scope is the location of the program where a variable can be used. A method’s scope is defined by “{}”. Everything inside them is within the method’s scope, meaning that variables in the scope can be used by the method.
Overloads: Overloaded methods are those that have the same name inside the same scope. The difference between them is their parameterList.
i.e.: CalculateArea (int base, int height){}
CalculateArea (int base, int height, string type){}
When the CalculateArea method is called, the compiler will define which one it is depending on its parameterList. The same method can’t have an overload with the same types and amounts. i.e.: WriteText(string title){}
WriteText(string body){}
Decision Statements:
Boolean operators are those that return true/false. The most common are:
ü == equal to
ü != not equal to
ü < less than
ü <= less or equal than
ü > more than
ü >= more or equal than
ü && (AND). The outcome of it is true only if BOTH expressions are true.
ü || (OR). The outcome of it is true if either one of the expressions is true.
If Statements:
If you want to choose between executing different block of codes, depending on the result of a Boolean expression, you can use an if statement.
Syntax:
if (Boolean expression){
block 1
}else{
block 2
}.
If statements can also be used in a cascade manner:
if (i==1){
block 1
}else if (i==2){
block 2
}
else if (i==3){
block 3
}
Else{nothing}.
By doing this you say: if the first statement isn’t true, then do the second one. If that one isn’t true do the third one. The last one is in case none of the statements are true.
Switch Statements:
An easier way of cascading if the statements are similar is a switch statement. You’re simply telling, if the expression is this one come here, if it’s this other one go there. i.e.:
Switch (intDay)
{case 1: do this
break;
case 2: do this
break;
case 3: do this
break;
default: do this
break;
}
This way you only go to one of the blocks. If none of them are equal then the default block is executed. The break keyword finishes the switch statement.
Rules for switch:
Ø You can only use them on primitive data types (such as int, string).
Ø "The case labels must be constant expressions"(Microsoft Visual C# 2005 Step by Step, John Sharp) (1, “1”). If they’re not use an IF statement.
Ø "Two case labels cannot have the same value."(Microsoft Visual C# 2005 Step by Step, John Sharp)
Ø You can use the same statements for more than one label. But you can’t use different statements for the same label.
(i.e.: case Monday:
case Tuesday: do this (this is correct)
Break;
case thursday:
do this
case friday: do this (this is NOT correct, )
Break; )
You need to stop the cascading, this is generally done by using BREAK.