Variables & Data Types
Every variable in C# has a type, and that type is fixed the moment the variable is declared. An int stays an int for its entire life — you can't quietly hand it a string later the way you could in a loosely-typed language.
Declaring a variable
A declaration is a type followed by a name, optionally followed by an initial value:
int age = 30;
string name = "Maria";
double price = 19.99;
bool isAvailable = true;
Console.WriteLine(name);
Console.WriteLine(age);
Console.WriteLine(price);
Console.WriteLine(isAvailable);
Maria 30 19.99 True
Notice isAvailable printed as True with a capital T — that's how C# renders booleans by default, and it's a common source of confusion if you're coming from a language that lowercases them.
The common types
int— a whole number, no decimal point (roughly ±2.1 billion).double— a floating-point number, the default choice for anything with a decimal.decimal— also decimal, but higher precision and slower; the right choice for money.bool—trueorfalse, nothing else.char— a single character, written in single quotes like'A'.string— text, written in double quotes.
var and type inference
Typing out int or string every time can feel repetitive when the value on the right already makes the type obvious. var lets the compiler figure it out for you — but don't mistake this for dynamic typing. The variable still gets a concrete, fixed type at compile time; var just saves you from spelling it out:
var age = 30;
var name = "Maria";
Console.WriteLine(age.GetType());
Console.WriteLine(name.GetType());
System.Int32 System.String
GetType() here is just a diagnostic tool to prove the point — you won't see it in ordinary code. System.Int32 is the full name behind the keyword int; they're the same type, just two ways of writing it.
Constants
A value declared with const is set once and can never be reassigned. The compiler enforces it, which makes const a good fit for values like tax rates or physical constants that should never drift accidentally somewhere deep in a codebase:
const double TaxRate = 0.08; double price = 50.00; double total = price + (price * TaxRate); Console.WriteLine(total);
54
price is declared as a double and you try to assign it a string somewhere later in a 2,000-line file, the program won't even compile — you find out immediately, at the exact line, instead of discovering it as a bug report three weeks after shipping.