Basics Lesson 4 of 15
C# Variables and Data Types
Declare C# variables with int, double, decimal, bool, char and string, use var, const and readonly, understand nullable types, and convert between types with casting and Convert.
Every C# variable has a type that decides what it can hold. C# is strongly typed: once a variable is an int, it stays an int. This catches many mistakes before the program runs.
Common types
| Type | Holds | Example |
|---|---|---|
int | Whole numbers (about ±2.1 billion) | int students = 42; |
long | Very large whole numbers | long population = 115_000_000; |
double | Decimals (fast, approximate) | double average = 88.75; |
decimal | Exact decimals, for money | decimal price = 12.50m; |
bool | true or false | bool passed = true; |
char | One character, single quotes | char section = 'A'; |
string | Text, double quotes | string name = "Ana"; |
string name = "Andres Bonifacio";
int gradeLevel = 12;
double average = 91.5;
bool isOfficer = true;
char section = 'B';
Console.WriteLine($"{name}, Grade {gradeLevel}-{section}");
Console.WriteLine($"Average: {average}, officer: {isOfficer}");
Output
Andres Bonifacio, Grade 12-B Average: 91.5, officer: True
Note that C# prints booleans as True and False with capital letters.
decimal for money
double d = 0.1 + 0.2;
decimal m = 0.1m + 0.2m;
Console.WriteLine(d);
Console.WriteLine(m);
Output
0.30000000000000004 0.3
double stores decimals in binary and has tiny rounding errors. decimal stores them exactly in base 10, so it's the right type for pesos, prices and banking. Decimal values need an m suffix: 25.50m.
var
var school = "Mabini NHS"; // string
var count = 1240; // int
var rate = 0.85; // double
var lets the compiler infer the type. The variable is still strongly typed; you just don't write the type. Use it when the type is obvious from the right side.
Constants
const int PassingGrade = 75;
const decimal Vat = 0.12m;
decimal price = 250m;
Console.WriteLine($"With VAT: {price * (1 + Vat)}");
Console.WriteLine($"Passing grade: {PassingGrade}");
Output
With VAT: 280.00 Passing grade: 75
C# convention names constants in PascalCase (PassingGrade). Notice the decimal result keeps two decimal places from 0.12m.
Integer division and casting
int total = 265;
int subjects = 3;
Console.WriteLine(total / subjects);
Console.WriteLine((double)total / subjects);
Console.WriteLine((int)9.99);
Console.WriteLine(Math.Round(9.99));
Output
88 88.33333333333333 9 10
int / int drops the decimals. Casting with (double) converts first. Casting a decimal number to int cuts off the decimals; use Math.Round to round.
Converting text
int a = int.Parse("85");
double b = double.Parse("12.5");
int c = Convert.ToInt32("40");
string d = 2026.ToString();
Console.WriteLine(a + c);
Console.WriteLine(b * 2);
Console.WriteLine(d + "!");
Output
125 25 2026!
Nullable types
Value types like int can't normally be null. Add ? to allow "no value":
int? finalGrade = null; // not graded yet
Console.WriteLine(finalGrade.HasValue);
finalGrade = 88;
Console.WriteLine(finalGrade ?? 0);
Output
False 88
?? is the null-coalescing operator: "use the left value, or the right one if the left is null".
Naming conventions
- Local variables and parameters: camelCase (
finalGrade). - Methods, classes, properties and constants: PascalCase (
ComputeTotal,Student,PassingGrade).
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04