beginner · 22 minute study
A declared type is a contract for every assignment
Read a C# program without treating its extra syntax as decoration. Understand which mistakes are caught before execution and which remain your responsibility.
Useful preparation · 1 prerequisite
Use this as a map, not a lock. If these ideas are familiar, continue.
- Turn a rule into a small, testable contract — Move from copying blocks of code to naming a useful operation with clear inputs, outputs and failure behavior.
First, build a mental model
A variable has a type even when you let the compiler infer it. C# source is compiled for .NET; a browser does not execute that source as JavaScript.
- int stores whole numbers; bool stores a decision; string stores text. Choose the representation for the operations you need.
- var means infer a static type from this initializer. It does not mean the type can change on the next line.
- Two int operands use integer division: 5 / 2 is 2. Introduce an appropriate fractional operand when the contract needs a fraction.
- decimal represents base-10 values with a finite range and precision; the m suffix selects a decimal literal. Rounding rules still need an explicit business decision.
int score = 10;
var bonus = 3; // bonus has type int
score += bonus;
int half = 5 / 2;
decimal price = 12.50m;
decimal total = price * 2;
Console.WriteLine(score);
Console.WriteLine(half);- Bind
score and bonus are both int. The inferred type of bonus stays int.
- Calculate
score becomes 13; integer division makes half equal 2.
- Validate
The compiler checks type compatibility. It does not decide whether half was the intended business result.
Production thinking
Take this beyond the example.
Use C# for a deliberate runtime and deployment target. Compare it with JavaScript's dynamic runtime and TypeScript's erased checks; do not assume the same numeric semantics across languages.
Sources and further reading
Your evidence
unseen is a local learning signal, not a credential or proof of production competence.
Explain the mental model, then explicitly acknowledge it. Opening a page is not evidence.
Supported retries can complete this lesson. Independent evidence and delayed review determine the skill stage.
Next lesson · Guard invalid cases before repeating useful work →