Lecture 2
Lecture 2
IN C#
~THARUKA PERERA
Lecture 02
Foundation Certification in IT – UOB batch 1
2
Naming Variables
Arithmetic Operator
Operator Precedence
3
COMPARING BUILT-IN AND USER-DEFINED VALUE TYPES
Data Types/Value Types
VARIABLES
6
WHAT’S HAPPEN INSIDE YOUR RAM?
DIFFERENCE BETWEEN STACK AND HEAP MEMORY
8
Rules
1. Must start with a letter or the underscore character.
2. After the first character use letters, digits or the underscore
character.
3. Do not used reserved keywords.
RULES AND Recommendations
RECOMMENDATIONS Avoid using all uppercase letters.
FOR NAMING
Avoid starting with an underscore.
VARIABLES
Avoid using abbreviations (shortened form of a word or
phrase).
Use PascalCasing naming in multiple-word names.
(PascalCasing : capitalize the first character of each word.)
9
C# KEYWORDS
10
11
KEYWORDS IN
C#
int 12count;
int __identifier;
12
Data Type Memory
TYPE CASTING int 4 bytes
float 4 bytes
Convert from one data type to another data type. double 8 bytes
char 2 bytes
Type Casting string 2 bytes per
character
bool 8-bit
Implicit Casting Explicit Casting
Smaller size convert to Large size Large size to Smaller size
No loss data Might be data loss
Type safe casting Unsafe casing
Ex:- Ex:-
• Int → float • String → char
• Int → double • Float → int
• Float → double • Double → int
• Char → int • Double → float
TYPE CASTING – CONT.
There are many Built-in methods to do the type casting such as,
• Convert.ToInt32() • int.Parse()
• Convert.ToChar() • double.Parse()
• Convert.ToDouble()
• Convert.ToString()
Addition +
Subtraction -
Multiplication *
Division /
Remainder %
Find the answer : 2+6/2*5+2-1*3
DIVISION AND REMAINDER
If both operands to the division operator (/) are integers, the result is an integer (the fractional part is
discarded)
14 / 3 equals?
8 / 12 equals?
14 % 3 equals?
8 % 12 equals?
OPERATOR PRECEDENCE
Precedence rules
Parenthesis are done first
Division, multiplication and modulus are done second
Left to right if same precedence (this is called associativity)
Addition and subtraction are done last
Left to right if same precedence
PRECEDENCE OF ARITHMETIC OPERATIONS
Operator(s) Operation Order of evaluation (precedence)
( ) Parentheses Evaluated first. If the parentheses are nested,
the expression in the innermost pair is
evaluated first. If there are several pairs of
parentheses “on the same level” (i.e., not
nested), they are evaluated left to right.
*, / or % Multiplication Evaluated second. If there are several such
Division operators, they are evaluated left to right.
Modulus
+ or - Addition Evaluated last. If there are several such
Subtraction operators, they are evaluated left to right.
Precedence of arithmetic operators.
OPERATOR PRECEDENCE: EXAMPLES
Identify the order of evaluation in the following expressions and Find the answer.
1. 2 + 3 + 4 + 5 + 6
2. 2 + 3 * 4 - 12 / 6
3. 12 / (3 * (2 + (5 - 3)))
4. 14 / (1 + 6) - 7 % 2
SEE YOU NEXT
WEEK
20