When C# evaluates an expression and returns a new value, it would be nice if the value could be stored somewhere, so we could use it later in the program. Otherwise, we'd need to keep typing the same expression to get the value.
In programming, we have a concept called a variable. A variable is a named representation of some value. Variables can hold values of the type it was declared with. For example, a
string variable can contain values of the
To declare a variable in C#, we need the type of the variable and a name to call it. We use the C# keyword in Lesson 3 to specify the type of the variable.
The statement above is called a declaration statement. We have declared a variable called
number that has the type of
int. This variable can hold integer numbers.
When we declare a variable, we can also assign a value to it.
int number = 50;
We can use this variable in a
int number = 50;Console.WriteLine(number);
Can you guess what the output will be? That's right, the console will display
If we don't assign a value to a variable, like the first statement in this lesson, the variable will have a default value. For numeric types like
int, that value is
= 50 from the program above and run it. The value in the output will be
Once you've declared a variable, we can assign a different value to it. The syntax will be similar to the declaration, except that we don't need to specify the type again. C# already knows its type from the declaration.
We can also assign expressions to a variable, like below:
number = 5 + 20;
This statement is an assignment statement. We have assigned a value to the
Remember that C# will evaluate expressions like
5 + 20 into its resulting value.
Type the program below into your code box. Can you guess what the output will be?
int number = 50;number = 5 + 20;Console.WriteLine(number);
If you answered
25, you're correct! The program won't display
50 because we assigned a new value to
number in Line 2.
An important thing to remember is that once a variable has been declared with a certain type, that type can't be changed. The value can be changed, but only values of its declared type are acceptable.
For example, we can't assign a
bool value to an
int number = "Will it blend?"; // This statement will cause an error!int anotherNumber;anotherNumber = false; // This statement will ALSO cause an error!
There's another way to declare a variable in C#. Instead of specifying the type ourselves, we can let C# guess the type for us.
var title = "Full Metal Alchemist";
var keyword is used to declare variables like
title above. But what is the data type of
Because we have assigned the value
"Full Metal Alchemist" to it, which is a
string value, the type of
string. Recall that strings are text values surrounded by double quotes (Lesson 3).
Since C# needs an initial value in order to guess the type of the variable, using the
var keyword means we can't declare without an assigned value.
var myVariable; // This will cause an error - C# doesn't know what type it is!