Data types and variables
Programs work with data: numbers, text, answers that are true or false. C# is statically typed, which means every piece of data has a type that is known when the program is compiled. The types you'll use most often at the start are:
| Type | Holds | Examples |
|---|---|---|
int | whole numbers | 3, -12 |
double | numbers with a fractional part | 3.14 |
char | a single character | 'a', 'Z', '?' |
string | text | "John Doe" |
bool | true or false | true, false |
The type matters. The number 12 and the string "12" are different things: you can do arithmetic on the number, but the string is just two characters.
A variable is a named container for a value. The value lives in memory, and the name is the label you use to get it back. You can picture a box with a label on it, where the box holds the value.
Declaring a variable
To declare a variable, write its type and then its name:
string name;
This creates a string variable called name. In C# the type always comes first.
Initializing a variable
You normally give the variable its first value in the same statement, with the assignment operator =:
string name = "AlgoCademy";
int age = 10;
Each line reserves memory, stores the value there and attaches the name to it. Reading from the variable is just a matter of using its name:
string name = "AlgoCademy";
int age = 10;
Console.WriteLine(name);
Console.WriteLine(age);
AlgoCademy
10
Notice there are no quotes around name inside WriteLine. With quotes, C# would print the word "name" instead of the variable's value.
Changing a value
A variable can be assigned again later, as long as the new value has the same type. Leave out the type this time, because the variable already exists:
int age = 10;
age = 11;
Console.WriteLine(age);
11
Gotchas in C#
- Always initialize. C# won't let you read a local variable that has no value yet. After
string name;, the lineConsole.WriteLine(name);fails to compile with an error like "Use of unassigned local variable 'name'". Giving every variable a starting value avoids the problem. - Types must match.
int age = "31";is rejected with "Cannot implicitly convert type 'string' to 'int'". Quotes make text, and anintvariable can't hold text. - Declare each name once. Writing
string a = "x"; string a = "y";in the same scope is an error. To change the value, assign without the type. - Names are case-sensitive.
ageandAgeare two different names. The usual style for local variables is camelCase, such asuserName. - Pick the right numeric type. An
intholds whole numbers up to 2,147,483,647. Going beyond that wraps around silently, and no error is raised. Alongholds much larger values. varis not a type of its own.var age = 31;lets the compiler work out the type, hereint, from the value. The variable still has a fixed type. This lesson spells the type out, which is clearer when learning.
The exercise
You need to create two variables, on two lines:
- A
stringvariable namednamewith the value"Michael". Text goes in double quotes, and the type comes before the name. - An
intvariable namedagewith the value31. A number gets no quotes, because"31"would be a string.
string name = "Michael";
int age = 31;
Nothing is printed, because the lesson only asks you to create the variables. To see them, add Console.WriteLine(name); and Console.WriteLine(age);, which print Michael and 31.
Key takeaways
- A variable is a named container for a value, and it has a type.
- Declare it with the type first:
type name = value;. - Common types are
int,double,char,stringandbool. - Initialize variables when you create them, since C# refuses to read an unassigned one.
- Reassign with
name = newValue;, using a value of the same type.