Skip to content

Variables and Types

The Course
•
25 min study

A variable is a named place to keep a value so you can use it later. In C#, every variable also has a type: the kind of value it may hold. You'll use four types constantly.

TypeHoldsExample
intwhole numbers25
doublenumbers with a fractional part2.5
booltrue or falsetrue
stringtext"Read chapter"

The declaration int minutes = 25; creates a variable called minutes, says it holds an int, and gives it the value 25. Names start with a lowercase letter by convention and use a capital for each new word, as in minutesPerDay.

Assignment replaces the old value

= does not mean "is equal to". It means "store the value on the right in the variable on the left". The right side is worked out first, using the variable's current value.

Trace the value line by line C#
int minutes = 25;
Console.WriteLine(minutes);

minutes = minutes + 5;
Console.WriteLine(minutes);

minutes = 10;
Console.WriteLine(minutes);
Check your prediction
Text
25
30
10

Only the first line says int. After that, minutes already exists, so declaring it again would be an error. Try adding int to the second assignment and read the message.

A type is a promise

Once a variable is declared, its type never changes. Storing text in an int isn't a problem that shows up later while the program runs; the compiler refuses to build it.

The compiler checks types C#
int minutes = "twenty";
Console.WriteLine(minutes);
Check your prediction
Text
error CS0029: Cannot implicitly convert type 'string' to 'int'

The quotes make all the difference: "25" is text and 25 is a number. Remove the quotes and change the value to a number to fix the program.

Let var work out the type

When the right side makes the type obvious, you may write var and let the compiler infer it. The variable is still exactly as strictly typed as before; var only saves you writing the type out. A const is a value that can never change, which suits a fixed rule such as a limit. By convention, its name starts with a capital.

var and const C#
const int MaxMinutes = 180;
var title = "Read chapter";
var minutes = 25;
var finished = false;

Console.WriteLine(title);
Console.WriteLine(minutes);
Console.WriteLine(finished);
Console.WriteLine(MaxMinutes);
Check your prediction
Text
Read chapter
25
False
180

Notice that a bool prints as False, with a capital letter, even though you write false in code. Now try MaxMinutes = 200; on a new line, and title = 5; on another. Both are compile errors, for different reasons. Explain each one in your own words.

A local variable must also be given a value before you read it. int total; followed by Console.WriteLine(total); is error CS0165, rather than printing whatever happened to be there.

Your turn

Exercise

Track a day's study

Create a string called topic holding Loops, an int called minutes holding 20 and a bool called done holding false. Print all three on separate lines. Then add 15 to minutes, set done to true and print those two again.

Your workspace C#
string topic = "Replace this";
Console.WriteLine(topic);
Hint 1: the idea

Declare each variable once with its type. Later changes use the existing name without a type in front.

Hint 2: a first step

Get the three declarations and the first three print statements working before you change any values.

Show one solution
C#
string topic = "Loops";
int minutes = 20;
bool done = false;

Console.WriteLine(topic);
Console.WriteLine(minutes);
Console.WriteLine(done);

minutes = minutes + 15;
done = true;

Console.WriteLine(minutes);
Console.WriteLine(done);
Text
Loops
20
False
35
True

Each variable's type matches what it holds: text, a whole number, and true or false. The later lines assign new values to the same variables, with no type in front, because declaring them again would be an error. minutes = minutes + 15 reads the old value first and then stores the new one.

Close the solution. Write the program again using var for all three variables, then explain why the behaviour stays the same.

Was this article helpful?

Your response is saved on this device.