Variables and data types — C#
What is this and why?
Imagine your computer is a huge cabinet with drawers. In each drawer you can store one thing: a toy, a book, or a notebook. A variable is such a drawer that has a name (e.g., "my_pencil_case"), and a data type is a rule about what exactly can be put into that drawer. If the drawer is labeled "toys," you can't put a book in there — it won't fit or it will break.
In programming, it's the same: a variable stores a value (a number, a word, true/false), and the data type tells the computer how to work with that value. For example, numbers can be added, and words can be concatenated. If you mix up types, the program can get "confused" and throw an error.
Why is this needed? So your program understands: "The variable age holds the number 12, and the variable name holds the text 'Masha'." Without data types, the computer wouldn't know if you can write age + name (it's like asking: "What is 12 plus Masha?" — nonsense!).
Syntax and code examples
In C#, a variable is declared like this: first write the data type, then the variable name, then the = sign and the value. At the end, a semicolon ; is mandatory — it's like a period at the end of a sentence.
// Example 1: Integer (int)
int age = 12; // "int" — integers: 0, 1, -5, 1000
// Now the variable age holds the number 12
// Can be used: age + 5 = 17
// Example 2: Floating-point number (double)
double price = 99.99; // "double" — numbers with a fractional part: 3.14, -0.5
// Note: In C#, the decimal separator is a DOT!
// price * 2 = 199.98
// Example 3: Text (string)
string name = "Anna"; // "string" — text in quotes
// Can be concatenated: "Hello, " + name = "Hello, Anna"
// Quotes must be double: " "
// Example 4: True or false (bool)
bool isStudent = true; // "bool" — only true or false
// Used in questions: "Are you a student?" — true
// Example 5: Mixing variables
int apples = 5;
int oranges = 3;
int totalFruits = apples + oranges; // totalFruits = 8
string message = "You have " + totalFruits + " fruits!";
// message = "You have 8 fruits!"
Important! You can come up with variable names yourself, but follow the rules: no spaces, start with a letter, use only Latin characters. It's better to use descriptive names: score, playerName, rather than x or a1.
Comparison with Python
In Python, data types also exist, but you don't need to specify them explicitly — Python figures it out on its own. This is convenient for small programs, but can be confusing when the program is large.
// C#: strictly specify the type
int number = 10;
string text = "Hello";
# Python: type is determined automatically
number = 10 # Python understood this is an int
text = "Hello" # And this is a str
The difference: in C#, you cannot write number = "ten" if you first declared int number — it will be an error. In Python, you can, but then you'll get confused. C# is like a strict teacher: it demands order. Python is like a friend: "Come on, it'll be fine." For beginners, C# helps avoid silly mistakes.
Typical beginner mistakes
- Forgot the semicolon — C# doesn't understand where the command ends without it. Error:
int x = 5(forgot;). - Mixed up quotes — text needs double quotes
" ", not single quotes' '(single quotes are for a single character). - Used Russian letters in the name — C# only likes English letters:
int возраст = 12;— error! Writeint age = 12; - Added a number and text —
5 + "apples"— C# doesn't know what to do. First convert the number to text:5.ToString() + "apples". - Floating-point number with a comma — in C#, the decimal part is separated by a dot:
double pi = 3.14;(not 3,14).
Summary
Variables and data types are the foundation of any program. You have learned:
- To store numbers (
int,double), text (string), and true/false (bool). - To declare variables in C# and Python.
- To avoid common pitfalls.
Now you can write a program that calculates your pocket money or greets a friend by name. Try it yourself: create a variable string myName = "Your name"; and output Console.WriteLine("Hello, " + myName); — this is your first step into the world of code!