C-SHARP - 2.2 The variables
A variable stores a value we want to reuse, display or modify in our program. Once declared with its type, its name, an = sign and the value to assign, it can be used elsewhere in the program. For example, we created a variable nom holding a name and an age variable holding an integer, then we concatenated several pieces — fixed strings between quotes, a + sign outside the quotes, and the variable — to produce the full output: Bonjour, je m'appelle Jibril et j'ai 18 ans.
What happens here? We concatenate several string literals with variables that themselves contain strings (or numbers converted automatically when added to strings). The + must always sit outside the quotes, otherwise it gets treated as a literal character and we get a result we did not want.
Why use variables at all?
- Reusability: write the name once, reuse it everywhere — change it in one place, the whole program updates.
- Dynamic input: later we will ask the user to type their name; the input is stored in a variable and the program reuses it the same way.
- Clarity: a meaningful variable name documents the intent ("age" is clearer than a number hardcoded somewhere).
Hardcoding everything between quotes would defeat the point of programming. The goal is to build programs that are reusable and that can adapt to different inputs without rewriting the code. In the next video we cover the different variable types in C# — int, string, bool, and the rest — and the situations where each one is the right tool.
Summary
This lesson explains how to create variables in C# and assign values to them using the equals operator. The video demonstrates string concatenation by combining multiple strings and variables—for example, concatenating "Bonjour, je m'appelle " with a name variable, "et j'ai " with an age variable, and " ans" to produce "Bonjour, je m'appelle Jibril et j'ai 18 ans." The key point emphasized is that the + concatenation operator must be placed outside quotation marks. The lesson concludes by explaining that variables enable you to create reusable programs where user input can be dynamically stored and used, rather than hardcoding fixed values.
Key points
- Variables in C# are created and assigned values using the equals sign (=) operator
- String concatenation uses the + operator to join strings and variables together, with the + symbol always placed outside quotation marks
- Multiple strings and variables can be chained together using concatenation to produce a complete output string
- Variables allow you to create reusable programs where data can change dynamically, rather than embedding fixed values directly in code
- The next lesson will cover different variable types available in C#
FAQ
What is the purpose of using variables in C#?
Variables enable you to create reusable programs by storing values that can change dynamically. Instead of hardcoding a person's name and age directly into a string, you can use variables to store these values and reuse the same code with different inputs.
Where should the + operator be placed when concatenating strings and variables?
The + operator must always be placed outside the quotation marks. If you place it inside the quotes, you will get a different result than intended.
What does the concatenation example demonstrate?
The example shows combining multiple elements: the string 'Bonjour, je m''appelle ' plus a name variable plus ' et j''ai ' plus an age variable plus ' ans' to create a complete greeting like 'Bonjour, je m''appelle Jibril et j''ai 18 ans.'