Skip to content
Theory · Variables and types · lesson 1 of 8

Variables

What you'll learn

What you'll learn:

  • declare variables and constants, give them values and change the values of variables;
  • explain why b keeps its old value after b = a and a = 7;
  • tell apart the built-in types int, long, double, decimal, bool, char and string;
  • convert values from one type to another: with a cast and with the methods int.Parse and Convert.ToInt32.
A box age stamped int holds a tile 30, while a long slip of text does not fit into it

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:

C#
string name = "Anna";
int age = 30;
Console.WriteLine(name); // Anna
Console.WriteLine(age); // 30
age = 31;
Console.WriteLine(age); // 31

Let's go through the example line by line:

  1. string name = "Anna"; makes a box name for text and puts the string "Anna" into it.
  2. int age = 30; makes a box age for whole numbers and puts 30 into it.
  3. Console.WriteLine(name) prints not the word name but what lies in the box: Anna.
  4. Console.WriteLine(age) prints 30.
  5. age = 31; puts a new value into age, and the old 30 is gone.
  6. 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:

  • int is the type: age can hold only whole numbers;
  • age is the variable's name;
  • = 30 is the initial value;
  • ; ends the statement.

You may leave out the initial value and put one in later, on a line of its own:

C#
int score;
score = 10;
Console.WriteLine(score); // 10

A 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: age and Age are 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?

C#
int a = 5;
int b = a;
a = 7;
Console.WriteLine(a); // 7
Console.WriteLine(b); // 5

Let's go step by step:

  1. int a = 5; puts 5 into the box a.
  2. int b = a; makes a box b and puts into it a copy of the value in a, that is, also 5.
  3. a = 7; changes only the box a.
  4. The program prints 7 and 5: b holds its own copy, and writing to a did 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.

LiveTwo separate boxesstep 1 of 10 · frames: 1

int a = 5 makes the box a and puts 5 into it.

Watch b after the line a = 7

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:

C#
int total;
Console.WriteLine(total); // ✗
TEXT
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 = a copies the value: after that a and b change independently.
  • You can read a variable only after a value has been put into it.