Learn C# · lesson 3 of 61 · about 6 minutes
Text and True/False
Name your hero with a string, and keep yes-or-no facts, like defending, in a bool.
What you'll learn
- Use string for text
- Use char for a single character
- Use bool, and know it prints as True and False
Name your hero
Numbers aren't enough. Your hero needs a name, and the battle needs yes-or-no facts, like whether the hero is defending. The type of a variable decides what it can hold:
| Type | Holds | Example |
|---|---|---|
| int | whole numbers | 42 |
| double | decimals | 1.5 |
| bool | true or false | true |
| char | one character | 'B' |
| string | text | "Ada" |
var lets the compiler work out the type from the value: var lives = 3; is an int. The type is still fixed afterwards.
A hero in five types
string hero = "Ada";
int level = 3;
double speed = 1.5;
char rank = 'B';
bool defending = false;
Console.WriteLine(hero + " L" + level + " speed " + speed + " rank " + rank);
Console.WriteLine("Defending: " + defending);Output
Ada L3 speed 1.5 rank B
Defending: FalseTip
You write true and false in lowercase in your code, but C# prints them as True and False, with capital letters.
Now try it yourself
This lesson is free in Codiqo, with 9 questions on what you just read. You can only learn to code by writing code, so it ends with a short program you write yourself, which we run and check straight away.