Module 1 · Variables, Types and Control Flow · Lesson 2 of 41
Variables, and the type system as an executable contract
Review task
Review a method that accepts object for task identity and dates. Predict the failed cast before running it, then propose a signature that rejects the wrong shapes at compile time. Keep a separate list of rules the compiler still cannot establish: existence, permitted dates and valid domain IDs.
Attempt before checking the reference
Case 1: Variables are typed names for runtime slots
Predict
After visibleTaskCount++, what exact value remains in each conceptual variable, and why?
Change
Rewrite a TaskFlow snippet containing variables named a, b, and temp. Choose names that state business meaning, then write the expected value of every variable after each assignment.
Constraint
Do not change behavior. Your refactor should make the prediction easier before the code runs.
Case 2: The type system is an executable contract
Predict
Which bugs can the compiler prevent in the good version, and which business rules still require runtime validation?
Change
Change a TaskFlow method that accepts object id and object dueAt into a signature using Guid and DateTimeOffset?. List one invalid call that now fails to compile.
Constraint
Do not add casts inside the method. The caller must satisfy the contract.
Check the decision
An object variable contains the string "42". The code casts it directly to int. What happens?
Show answer
Correct answer: C
The boxed/reference target is a string, not a boxed int. Use an appropriate parsing boundary when conversion from text is intended.
Hint: Distinguish the declared reference type from the object’s runtime type.
Trace the contract
| Boundary | Question to answer |
|---|---|
| Assignment | Which value was copied into the destination variable? |
| Cast from object | What runtime type does the object actually contain? |
| Typed method signature | Which argument shapes can the compiler reject? |
| Domain acceptance | Which business predicates still need runtime checks? |
Reference cases
Module 1: How C# Really Thinks
Project milestone
TaskFlow can capture a task draft, pass values through methods, and explain exactly what is copied at each assignment and call.
Variables are typed names for runtime slots
One concept
For the ordinary simple assignments in this lesson, the right-hand value is copied into the destination variable. A reference-type value is a reference, not a copy of the referenced object.
Mental model
Model these local variables as named, typed storage locations. This is a semantic model, not a promise that every source-level local occupies a separate physical stack slot: the JIT may use registers or optimize storage away. Ordinary assignment copies the value; it does not make two variable names aliases. Explicit ref locals and ref assignment are a different feature, outside these examples.
MENTAL MODEL DIAGRAM
| source code | conceptual local values |
|---|---|
int pendingCount = 3; | pendingCount slot: [ 3 ] |
int visibleCount = pendingCount; | visibleCount slot: [ 3 ] |
visibleCount++; | pendingCount slot: [ 3 ] visibleCount slot: [ 4 ] |
TaskFlow: bad vs good
BAD - meaningless names hide the data contract
var x = 3; var y = x; y++; Console.WriteLine(x);
GOOD - names expose intent and types expose the contract
int pendingTaskCount = 3; int visibleTaskCount = pendingTaskCount; visibleTaskCount++; Console.WriteLine(pendingTaskCount); // 3
What beginners get wrong
They imagine both names point to one universal box, or they use var plus single-letter names until the code stops communicating intent. var is not the problem; invisible meaning is.
What breaks in production
A later refactor changes the wrong variable because names are vague, or a developer assumes two locals remain synchronized after assignment.
The type system is an executable contract
One concept
Static types let the compiler reject many invalid assignments and operations before execution. They do not prove that all runtime values satisfy business rules, and some operations (such as a downcast or dynamic binding) require runtime checks.
Mental model
A type is not decoration. It is a contract between the producer, consumer, compiler, and runtime. The narrower and more truthful the type, the fewer invalid operations are expressible. The compiler is your first code reviewer, but only if you give it useful contracts.
MENTAL MODEL DIAGRAM
producer ---- value ----> consumer | v +---------------+ | type contract | +---------------+ allowed shape allowed operations nullability annotations
TaskFlow: bad vs good
BAD - object and string erase useful meaning
object taskId = "42"; object dueAt = "tomorrow"; int id = (int)taskId; // runtime failure
GOOD - specific types move many invalid uses to compile time
Guid taskId = Guid.NewGuid(); DateTimeOffset? dueAt = null; Console.WriteLine(taskId); Console.WriteLine(dueAt?.ToString() ?? "No due date");
Here the object variable actually refers to a string, so casting it to int throws InvalidCastException; a cast is not parsing. Guid and DateTimeOffset? make the intended representation explicit, but rules such as a nonempty ID or an allowed due date still need validation. Nullable reference annotations likewise guide compile-time analysis; they do not insert runtime null checks. DateTimeOffset? here is instead a nullable value type that can genuinely represent no date.
What beginners get wrong
They select a type because it is convenient to store, not because it states the domain rule. object and string become escape hatches that postpone failure.
What breaks in production
Invalid data crosses layers and fails far from its source through casts, parsing, culture differences, or swapped parameters.
TechInterviewNotes Editorial Team · Updated 28 Oct 2026 ·