Coding/Learn/csharp/variables and types
C# Foundational Course
01.Why C# & .NET Power Modern Software
02.Anatomy of a C# Application
03.Variables & The C# Type System
04.Console I/O: Reading & Writing
05.String Interpolation & Modern Formatting
06.Arithmetic, Math Class & Modulo
07.Conditional Branching: if, else if, and else
08.Logical Operators (&&, ||, !)
09.Switch Statements & Modern Switch Expressions
10.While & Do-While Loops
11.The Standard For Loop & Counting
12.Loop Control: break and continue
13.Methods: Modular, Reusable Code Blocks
14.Optional Parameters, Named Arguments & out/ref
15.Fixed-Size Arrays & Zero-Based Indexing
16.The Sacred Foreach Loop
17.Dynamic Collections: List<T> & Generics
18.Classes & Objects: The OOP Blueprint
19.Auto-Properties & Constructors
20.Capstone: RPG Character & Inventory Engine
Course Certificate0%

Complete all lessons to earn

LearnVariables & The C# Type System
3 / 20 0%
Lesson 03 of 20
5 mins read

Variables & The C# Type System

PreviousNext Lesson
In Plain English

C# is a strongly typed language where every variable has a defined type. C# offers rich primitives for numbers (int, double, decimal), text (char, string), boolean logic (bool), and smart type inference using the var keyword.

The Mental Model

"Variables are like labeled storage containers. You have a rigid steel box for whole bricks (int), a glass beaker for liquids with decimals (double), a cash register drawer for financial currency (decimal), and an envelope for letters (string)."

Deep Dive: How It Works

int: 32-bit whole integer (e.g. 42, -500).

double: 64-bit floating-point number (e.g. 3.14159). Fast, great for graphics and scientific math.

decimal: 128-bit ultra-precise decimal (e.g. 19.99m). Mandatory for financial and currency calculations to prevent floating-point rounding errors! (Requires "m" suffix).

bool: true or false.

char: Single Unicode character in single quotes (e.g. 'A', '#').

string: Sequence of characters in double quotes (e.g. "ZenCompiler").

var: Implicit typing—the compiler infers the exact type automatically from the assigned value (e.g. var speed = 100; becomes an int).

Syntax Blueprint

int score = 95;
double speed = 88.5;
decimal price = 14.99m;
bool isReady = true;
string name = "Neo";
var count = 10; // Inferred as int

Explicit typing (<type> <name> = <val>;) or implicit typing with var.

PointCore Rules to Remember

Point star
Strongly Typed: Variables cannot arbitrarily change type after being declared.
Point star
decimal for Money: Always use decimal with the 'm' suffix for money to avoid rounding glitches.
Point star
var is Still Statically Typed: var is not dynamic; the compiler locks in the type at compile time.

Common Beginner Traps & How to Fix Them

Mistake:Forgetting the 'm' suffix on decimal: decimal price = 9.99;

Why it happens: C# defaults decimal numbers to double without a suffix.

How to fix: Always write decimal price = 9.99m;.

Pro Tip: Use var when the variable type is obvious from the right side of the assignment (e.g. var list = new List<string>();)!

Live Interactive Example

Hit Run Code to see it live
Declaring and Outputting C# Types
C# 12 / .NET
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Output Console
Click "Run Code" to view the rendered output.
How it works: C# automatically formats numbers, booleans, and decimals when concatenated with strings.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Declare player variables

  • PointDeclare string playerName = "Valkyrie";
  • PointDeclare int health = 100;
  • PointPrint "Player Valkyrie has 100 HP" by concatenating these variables.
1
2
3
4
5
6
7
8
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates