Variables
What you'll learn:
- declare variables and constants, give them values and change the values of variables;
- explain why
bkeeps its old value afterb = aanda = 7; - tell apart the built-in types
int,long,double,decimal,bool,charandstring; - convert values from one type to another: with a cast and with the methods
int.ParseandConvert.ToInt32.

Picture a box with a label. The label carries a name, and the box holds a value. By its name you can find the box, look inside it or put something else into it. A program keeps its data the same way—in variables: each one has a name and a value you can read and replace.
A box also has a shape. A box for whole numbers takes no text, and a box for text takes no number. In C# the shape of the box is called its type, and you choose it as soon as you make the box.
Why give values names? A user's name, their age, a score in a game—a program uses each such value in several places. Once a value has a name, you can print it, replace it and print it again:
string name = "Anna";
int age = 30;
Console.WriteLine(name); // Anna
Console.WriteLine(age); // 30
age = 31;
Console.WriteLine(age); // 31Let's go through the example line by line:
string name = "Anna";makes a boxnamefor text and puts the string"Anna"into it.int age = 30;makes a boxagefor whole numbers and puts30into it.Console.WriteLine(name)prints not the wordnamebut what lies in the box:Anna.Console.WriteLine(age)prints30.age = 31;puts a new value intoage, and the old30is gone.- The last line prints
31.
Declaration
The line int age = 30; declares a variable: it tells the compiler the variable's type and name. Let's take it apart:
intis the type:agecan hold only whole numbers;ageis the variable's name;= 30is the initial value;;ends the statement.
You may leave out the initial value and put one in later, on a line of its own:
int score;
score = 10;
Console.WriteLine(score); // 10A variable must be declared before its first use, higher up in the program. If there is no declaration at all, the compiler rejects the line score = 10;: it does not know the name score and reports error CS0103.
Variable names
A variable name follows three rules:
- it starts with a letter or
_, followed by letters, digits and_; - letter case matters:
ageandAgeare two different variables; - a keyword of the language, such as
int, cannot be a name: C# has already given it a meaning.
Besides the rules there is a convention accepted in .NET. Compound variable names are written as one word, and every word after the first starts with a capital letter: itemCount, maxSpeed.
Assignment
The = sign performs assignment: it takes the value on its right and puts it into the variable on its left. What happens when the right side is another variable?
int a = 5;
int b = a;
a = 7;
Console.WriteLine(a); // 7
Console.WriteLine(b); // 5Let's go step by step:
int a = 5;puts5into the boxa.int b = a;makes a boxband puts into it a copy of the value ina, that is, also5.a = 7;changes only the boxa.- The program prints
7and5:bholds its own copy, and writing toadid not touch it.
Assignment does not tie b to a. It copies the value a holds at that moment, and from then on the two variables live separately.
Press "Next step" and watch b: after the line a = 7 its value stays the same.
1int a = 5;2int b = a;3Console.WriteLine(a);4Console.WriteLine(b);5a = 7;6Console.WriteLine(a);7Console.WriteLine(b);8b = 9;9Console.WriteLine(a);10Console.WriteLine(b);
int a = 5 makes the box a and puts 5 into it.
Here the box analogy stops working. A real object cannot sit in two boxes at once: move it, and the first box is empty. Assignment moves nothing; it copies: after b = a each box holds its own five, and a does not become empty.
Interviewers like to ask this: does b change if you change a after b = a? For int the answer is no: the assignment copied the number itself.
Common mistake
The variable was declared, but nobody put a value into it:
int total;
Console.WriteLine(total); // ✗error CS0165: Use of unassigned local variable 'total'The program does not compile. The compiler makes sure a variable gets a value before anyone reads it: there is nothing to look at in an empty box. Give it an initial value when you declare it: int total = 0;.
Key points
- A variable is a box with a name and a value; its type is set when you declare it:
int age = 30;. =puts the value on its right into the variable on its left, and the old value is gone.b = acopies the value: after thataandbchange independently.- You can read a variable only after a value has been put into it.