Variables and Data Types
A variable is a labelled box that holds a value. In C#, every box has a type that says what can go inside — and the compiler enforces it.
On this page · 5 min · Beginner
On this page
- In simple words
- Learning objectives
- Explanation
- Syntax
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Beginner
- Updated
- Oct 28, 2026
Related tutorials
In simple words#
A variable is a labelled box on a shelf. The data type is the shape of the box: an int box holds whole numbers, a string box holds text, a bool box holds only yes/no. You cannot put a sentence into a number box.
- Variable
- A named place to store a value.
- Data type
- What kind of value it may hold (int, string, bool, decimal…).
- var
- Let the compiler work out the type from the value.
Learning objectives#
- Declare variables with a type
- Choose between int, long, double and decimal
- Use bool, char and string
- Use var and const correctly
Explanation#
Declare a variable as type name = value; for example int age = 21;. Once declared, the type cannot change: you cannot later put text into age.
Whole numbers: int (about ±2.1 billion) and long (much larger). Decimal numbers: double for science and measurements, decimal for money because it stores 0.1 exactly.
bool holds true or false. char holds one character in single quotes ('A'). string holds text in double quotes ("Pune").
var lets the compiler infer the type from the value: var count = 3; is still an int. It is not “any type” — it is just shorter.
const declares a value that can never change, such as a tax rate. Number literals have suffixes: 499.99m is a decimal, 3.5f a float, 10L a long.
Syntax#
int count = 10;
double weight = 72.5;
decimal price = 199.99m; // m = decimal
bool isActive = true;
char grade = 'A';
string city = "Pune";
var total = 5; // inferred as int
const double Pi = 3.14159;
Code example: Prices, flags and text#
int age = 21;
double rating = 4.5;
decimal price = 499.99m;
bool isMember = true;
char grade = 'A';
string city = "Pune";
var quantity = 3; // the compiler infers int
const decimal GstRate = 0.18m;
decimal total = price * quantity * (1 + GstRate);
Console.WriteLine(quot;Age {age}, rating {rating}, grade {grade}, member {isMember}");
Console.WriteLine(quot;{quantity} items in {city}: {total:F2}");
Console.WriteLine(int.MaxValue);
Console.WriteLine(0.1 + 0.2); // double: tiny rounding error
Console.WriteLine(0.1m + 0.2m); // decimal: exact
Age 21, rating 4.5, grade A, member True 3 items in Pune: 1769.96 2147483647 0.30000000000000004 0.3
This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.
Step-by-step explanation#
- Each variable is declared with its type, so the compiler knows what it can hold.
- price uses the m suffix, which makes the literal a decimal — right for money.
- quantity uses var, but its type is still int because 3 is an int.
- const GstRate can never be reassigned.
- {total:F2} formats the result with exactly 2 decimal places.
- The last two lines show why money uses decimal: double cannot store 0.1 exactly.
Real-world use case#
An invoice service stores amounts as decimal, quantities as int, the paid flag as bool and the customer name as string. Using double for money would create tiny rounding errors that add up across thousands of invoices.
Common mistakes#
0.1 + 0.2 is 0.30000000000000004 with double. Use decimal for currency.
decimal price = 9.99; does not compile: 9.99 is a double literal. Write 9.99m.
int x; Console.WriteLine(x); gives “Use of unassigned local variable”. Give local variables a value first.
Best practices#
- Use decimal for money, double for measurements, int for counts
- Use var when the type is obvious from the right side
- Name variables in camelCase and constants in PascalCase
- Declare variables close to where they are used
Remember this#
Money → decimal (with m). Measurements → double. Counting → int.
var is still strongly typed — the compiler picks the type once.
'A' is a char, "A" is a string.
Practice exercise#
Your task: Store a product name, its unit price (decimal) and a quantity (int). Print the total price with 2 decimal places.
Need a hint?
Show solution
string product = "Notebook";
decimal unitPrice = 45.50m;
int quantity = 4;
decimal total = unitPrice * quantity;
Console.WriteLine(quot;{quantity} x {product} = {total:F2}"); // 4 x Notebook = 182.00
Mini quiz#
Which type should store a product price?
Want more? Practise all C# MCQs · C# interview questions
My notes#
Saved in this browser.