Variables and types
Variables and data types
A variable is a named "box" in memory that holds a value of a specific type.
int age = 20;
double price = 4.99;
string name = "Ana";
bool isReady = true;
| Type | Example | Used for |
|---|---|---|
int |
42 |
whole numbers |
double |
3.14 |
real numbers |
string |
"text" |
text |
bool |
true / false |
logical values |
Reading from the console
Console.ReadLine() always returns a string. To use it as a number, convert it:
int n = int.Parse(Console.ReadLine());
Console.WriteLine(n * 2);
Integer division
Console.WriteLine(7 / 2); // 3 (both operands are int)
Console.WriteLine(7 / 2.0); // 3.5
Console.WriteLine(7 % 2); // 1 (remainder)
The most common mistakes
int x = "5";— a string can't be assigned to anintwithout conversion.- Expecting
3.5from7 / 2— both operands are integers, so the result is3. int.Parsethrows on invalid input; for untrusted input useint.TryParse.
Practice
Solve "Sum of two numbers" and "Area of a rectangle" — both combine console input with arithmetic.