0% found this document useful (0 votes)
2 views20 pages

Lecture 2

This document covers foundational concepts in C# programming, including variable naming, built-in data types, memory management, and type casting. It explains the differences between stack and heap memory, arithmetic operations, and operator precedence. Additionally, it provides rules for naming variables and highlights the importance of avoiding reserved keywords in variable names.

Uploaded by

used.subtype-4e
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views20 pages

Lecture 2

This document covers foundational concepts in C# programming, including variable naming, built-in data types, memory management, and type casting. It explains the differences between stack and heap memory, arithmetic operations, and operator precedence. Additionally, it provides rules for naming variables and highlights the importance of avoiding reserved keywords in variable names.

Uploaded by

used.subtype-4e
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

PROGRAMMING

IN C#
~THARUKA PERERA

Lecture 02
Foundation Certification in IT – UOB batch 1
2
Naming Variables

Using Built-in Data Types

OVERVIEW Using Methods

Arithmetic Operator

Operator Precedence

3
COMPARING BUILT-IN AND USER-DEFINED VALUE TYPES
Data Types/Value Types

Built-in Type User-Defined

◼ Examples of ◼ Examples of user-


built-in value defined value types:
types:
◼ enum
◼ int
◼ struct
◼ float
◼ String 4
BUILT IN DATA TYPES USED IN C#

Data Type Memory Used for Examples


int 4 bytes Stores whole numbers 2, 856974, 0
float 4 bytes Stores fractional numbers. 2.365, 3.0
Sufficient for storing 6 to 7
decimal digits
double 8 bytes Stores fractional numbers. 2.36589, 3.00001
Sufficient for storing 15
decimal digits
char 2 bytes Stores a single ‘a’, ‘*’
character/letter
string 2 bytes per A sequence of Unicode “Hello”, “C# Programming”
character characters
bool 8-bit Logical True / False TRUE , FALSE
5
 Variable is a Label/identification Name for data in
programming.
 Use to store information inside your Ram.
 Each variable has Data Type and a Name

VARIABLES

6
WHAT’S HAPPEN INSIDE YOUR RAM?
DIFFERENCE BETWEEN STACK AND HEAP MEMORY

Stack Memory Heap Memory


Execution of Used only by one thread of execution Used until the application exit.
Memory
Access Memory Can’t access by other threads Objects can access throughout the
application
Life Time Keep until end of the thread Keep until end of the whole application
execution execution
Usage To contain, To contain object details
• local primitive variables
• reference variable to objects in
heap area

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

abstract, base, bool, default, if, finally

 Keywords are reserved identifiers

 Do not use keywords as variable names


 Results in a compile-time error

 Avoid using keywords by changing their case


sensitivity int INT; // Poor style

10
11

KEYWORDS IN
C#
int 12count;

QUIZ: CAN YOU char $diskPrice;


SPOT THE
DISALLOWED char middleInitial;
VARIABLE
NAMES?
float this;

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()

Try this - int a = 5;


double b = 3.25;
bool c = true;
char x = 'x';

Console.WriteLine("Convert int to String = "+Convert.ToString(a));


Console.WriteLine("Convert int to double = "+Convert.ToDouble(a));
Console.WriteLine("Convert double to int = "+Convert.ToInt32(b));
Console.WriteLine("Convert bool to string = "+ Convert.ToString(c));
Console.WriteLine("Convert char to string = "+ Convert.ToString(x));
ARITHMETIC EXPRESSIONS

 An expression is a combination of operators and operands

 Arithmetic expressions (we will see logical expressions later)


compute numeric results and make use of the arithmetic operators:

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

 Operators can be combined into complex expressions

result = total + count / max - offset;


 Operators have a well-defined precedence which determines the order in which they are evaluated

 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

You might also like