C# Data Types
- Tells the computer what kind of value a variable can store.
- It is important to use the correct data type for the corresponding variable; to
avoid errors, to save time and memory, but it will also make your code more
maintainable and readable. The most common data types are:
name description example
int Stores whole numbers -32, -1, 0, 4, 125
double Stores fractional -3.5452345524435,
numbers. Sufficient 0.245524223,
for storing 15 decimal 12.52324535234
digits
char Stores a single ‘7’, ‘A’, ‘$’, ‘a’, ‘z’, ‘R’
character/letter/symbol/
nu mber, surrounded by
single quotes
bool (boolean) Stores true or false values true, false
string Stores a sequence of “Hello World!!”,
characters, surrounded “John Doe”,
by double quotes “123fourfivesix”
float Stores fractional -3.332245,
numbers. Sufficient 0.2332,
for storing 6 to 7 32.123918
decimal digits
Expressions
- a combination of values, variables, operators, and functions that produces a
result. In other words, it’s like a formula the computer can evaluate. - An
expression in C# is any valid piece of code that the computer can calculate to get
a value, like a math equation or a logical check
Examples:
1 + 3 * 5 2 / 3 - 5 (3-2) * 8
Arithmetic Operators
+ addition
- subtraction
* multiplication
/ division
% modulus(remainder)
++ increment
-- decrement
Integer Division With /
- When we divide integers, the quotient is also an integer.
- 14 / 4 is 3, not 3.5
- Dividing by 0 causes an error when your program runs.
Integer Remainder With %
- The % operator computes the remainder from integer division. -
14 % 4 is 2
- 218 % 5 is 3
- Applications of % operator:
- Obtain last digit of a number: 230857 % 10 is 7
- Obtain last 4 digits: 658236489 % 10000 is 6489 - See whether a
number is odd: 7 % 2 is 1, 42 % 2 is 0
Precedence
- Order in which operators are evaluated.
- Generally operators evaluate left-to-right.
- 1 - 2 - 3 is (1 - 2) - 3 which is -4
- But * / % have a higher level of precedence than + -
6 + 8 / 2 * 3 —-> 6 + 4 * 3 —--> 6 + 12 is 18
- Parentheses can force a certain order of evaluation:
(1 + 3) * 4 is 16
- Spacing does not affect order of evaluation
1+3 * 4-2 is 11
Decimal Numbers Operation (Double or Float)
- Examples: 6.022 , -42.0 , 2.143e17
- Placing .0 or . after an integer makes it a double or float.
- The operators + - * / % () all still work with double and float. -
A division / produces an exact answer: 15.0 / 2.0 is 7.5
- Precedence is the same: () before * / % before + -
Mixing types
- When int and double are mixed, the result is a double. -
4.2 * 3 is 12.6
- The conversion is per-operator, affecting only its operands.
Example:
7 / 3 * 1.2 + 3 / 2
2 * 1.2 + 3 / 2
2.4 + 3 / 2
2.4 + 1
3.4
String Concatenation
- Using + between a string and another value to make a longer string.
Examples:
"hello" + 42 is "hello42"
1 + "abc" + 2 is "1abc2"
"abc" + 1 + 2 is "abc12"
1 + 2 + "abc" is "3abc"
"abc" + 9 * 3 is "abc27"
"1" + 1 is "11"
4 - 1 + "abc" is "3abc"
- Use + to print a string and an expression's value together. -
Console.WriteLine("Grade: " + (95.1 + 71.9) / 2);
- Output: Grade: 83.5
Variables
- A piece of the computer's memory that is given a name and type, and can
store a value.
- Each variable has:
- Name (identifier you choose)
- Type (defines what kind of data it can store)
- Value (the actual data inside)
Example:
string name = “John”;
char alphabet = ‘Z’;
int number = -32;
double fraction = 3.14;
float num = 3.1415f;
bool isCorrect = false;
- Once given a value, a variable can be used in expressions.
Example:
int x;
x = 3;
Console.WriteLine("x is " + x); // x is 3
Console.WriteLine(5 * x - 1); // 5 * 3 - 1
- You can assign a value more than once.
Example:
int x;
x = 3;
Console.WriteLine("x is " + x); // x is 3
x = 3 * 4;
Console.WriteLine("x is now" + 12); // x is now 12
- = means, "store the value at right in variable at left"
- The right side expression is evaluated first, and then its result is stored in the
variable at left.
- A variable can only store a value of its own type.
int x = 2.5; // ERROR: incompatible types
- An int value can be stored in a double variable. The value is converted into
the equivalent decimal value.
double x = 6; // Computer reads 6.0 but the output is only 6 - A
variable can't be used until it is assigned a value.
int x;
Console.WriteLine(x); // ERROR: x has no value
- You cannot declare the same variable twice.
int x = 5;
int x = 7; // ERROR: x already exists - Use + to print a string
and a variable's value on one line.
double grade = (95.1 + 71.9 + 82.6) / 3.0;
Console.WriteLine("Your grade was " + grade);
int students = 11 + 17 + 4 + 19 + 14;
Console.WriteLine("There are " + students + " students in the course.");
Output:
Your grade was 83.2
There are 65 students in the course.
Constant
- If you don't want others (or yourself) to overwrite existing values, you can add
the const keyword in front of the variable type.
- This will declare the variable as "constant", which means unchangeable and
read-only:
const int myNum = 15;
myNum = 20; // error since we want to change 15 to 20
C# Identifier
- An identifier is the name you give to variables, methods, classes, or other
items in your program.
- Identifiers can be short names (like x and y) or more descriptive names (age,
sum, totalVolume).
- Rules for Identifiers in C#:
1. Must start with a letter or underscore (_).
✔️ name, _score
❌ 1number (cannot start with a digit)
2. Can contain letters, digits, and underscores.
✔️ age1, student_name
❌ my-name (hyphen not allowed)
3. Cannot be a C# keyword (like int, class, if).
❌ int int = 10; (not valid)
4. Identifiers are case-sensitive. Name
and name are different.
5. Should be meaningful.
✔️ studentAge (good)
❌ x (bad, unclear meaning)