Lesson 2 of 34 · Fundamentals
C# Variables and Data Types
Programs remember things in variables. A variable is a named box that holds a value — a hero's name, a score, whether a door is locked. In C# every variable also has a type that decides what kind of value fits in the box, and the compiler makes sure you never put the wrong thing in.
Declaring variables
You declare a variable by writing its type, a name and (usually) an initial value:
string name = "Aria";
int level = 7;
double health = 92.5;
bool isAlive = true;
level = level + 1; // variables can change
Console.WriteLine(level); // 8
Variable names use camelCase by convention: maxHealth, goldCoins, isAlive.
The everyday types
| Type | Holds | Example |
|---|---|---|
int |
whole numbers (±2.1 billion) | int coins = 250; |
long |
bigger whole numbers | long stars = 9_000_000_000L; |
double |
numbers with a fraction | double speed = 4.75; |
decimal |
exact decimal numbers — money | decimal price = 19.99m; |
bool |
true or false |
bool hasKey = false; |
char |
a single character, in single quotes | char rank = 'S'; |
string |
text, in double quotes | string title = "Knight"; |
Notice the suffixes: 19.99 on its own is a double, so a decimal needs an m (19.99m) and a large long literal can take an L. Use decimal for money: double is fast but stores fractions in binary, so 0.1 + 0.2 prints 0.30000000000000004.
Type inference with var
When the type is obvious from the value, you can let the compiler infer it with var:
var city = "Sharpforge"; // string
var walls = 12; // int
var height = 30.5; // double
var is still strongly typed: city is a string forever, and city = 5; will not compile.
Constants
Values that never change are declared with const:
const int MaxLevel = 99;
// MaxLevel = 100; // compile error: a constant cannot be changed
Converting between types
C# converts automatically only when nothing can be lost, such as int to double. Going the other way needs an explicit cast, which cuts off the fraction:
int whole = 7;
double precise = whole; // 7.0 — automatic, safe
double damage = 9.8;
int rounded = (int)damage; // 9 — the cast drops .8
int parsed = int.Parse("42"); // text to number
string text = 42.ToString(); // number to text
Printing values
Console.WriteLine can print any type. Combine text and values with +:
int coins = 250;
bool hasKey = true;
Console.WriteLine("Coins: " + coins); // Coins: 250
Console.WriteLine("Key: " + hasKey); // Key: True
A bool prints as True or False with a capital letter. And be careful with maths inside text: "Total: " + 2 + 3 gives Total: 23, because once the left side is a string, + joins text. Write "Total: " + (2 + 3) instead.
Common mistakes
int count = 3.5;— adoubledoes not fit in anintwithout a cast.decimal price = 9.99;— missing themsuffix.char letter = "A";— acharneeds single quotes:'A'.- Using a variable before giving it a value:
int x; Console.WriteLine(x);is a compile error.
Assignment
The quartermaster of the Citadel needs a hero card. Declare six variables with the right types:
name=Aria,level=7,health=92.5gold=1250.75(money, so usedecimal),isAlive=true,rank=S
Then print exactly these lines. The last line must be calculated from level:
Name: Aria
Level: 7
Health: 92.5
Gold: 1250.75
Alive: True
Rank: S
Next level: 8Variables and data types in other languages
- JavaScript Variables: let, const and Data Types · JavaScript
- Python Variables and Data Types · Python
- Go Variables: var, := and Multiple Assignment · Go
- Java Variables and Primitive Data Types · Java
Need a quick reminder? The C# cheat sheet has every topic of Learn C# on one page.
Lesson last updated on .