Skip to content
Search lessons, topics, tests…
Esc

    ↑ ↓ moveEnter openEsc close

    Guided course · Coding and System-Design Practice in C#

    TaskFlow C# Engineering Workbook: back to the course

    Module 1 · Variables, Types and Control Flow · Lesson 1 of 37

    Variables, and the type system as an executable contract

    Variables are typed names for runtime slots

    One concept

    Assignment copies the current contents of one typed slot into another slot.

    Why this matters in TaskFlow

    TaskFlow will move counts, titles, ids, and object references through many methods. You must know whether an assignment creates a new value or merely copies a reference before mutation enters the picture.

    Mental model

    At source level, a variable is a meaningful label. During a method call, the runtime gives that label a slot. The slot stores either a value directly or a reference to an object. Assignment copies the slot contents; it does not create a magical connection between variable names.

    MENTAL MODEL DIAGRAM

    source coderuntime frame
    int pendingCount = 3;pendingCount slot: [ 3 ]
    int visibleCount = pendingCount;visibleCount slot: [ 3 ]
    pendingCount;
    visibleCount++;pendingCount slot: [ 3 ] visibleCount slot: [ 4 ]

    TaskFlow: bad vs good

    BAD - meaningless names hide the data contract

    C#
    var x = 3;
    var y = x;
    y++;
    Console.WriteLine(x);

    GOOD - names expose intent and types expose the contract

    C#
    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 interviewers test

    Predict the output after assignments and mutations, then explain what was copied without saying only 'because value type.'

    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.

    Engineer question - answer before running code

    After visibleTaskCount++, what exact data remains in each runtime slot, and why?

    Mini refactor task

    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.

    The type system is an executable contract

    One concept

    A type constrains which values and operations are legal before the program runs.

    Why this matters in TaskFlow

    TaskFlow has task ids, titles, priorities, and timestamps. Treating them as interchangeable strings creates bugs the compiler cannot help you find.

    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

    Text
    producer ---- value ----> consumer
       |
       v
     +-------------+
     | type contract|
     +-------------+
     allowed shape
     allowed operations
     nullability promise

    TaskFlow: bad vs good

    BAD - object and string erase useful meaning

    C#
    object taskId = "42";
    object dueAt = "tomorrow";
    int id = (int)taskId; // runtime failure

    GOOD - specific types move failure to compile time

    C#
    Guid taskId = Guid.NewGuid();
    DateTimeOffset? dueAt = null;
    Console.WriteLine(taskId);
    Console.WriteLine(dueAt?.ToString() ?? "No due 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 interviewers test

    Why strong typing helps, where static typing stops, and why a domain-specific type can be safer than a primitive even when both compile.

    What breaks in production

    Invalid data crosses layers and fails far from its source through casts, parsing, culture differences, or swapped parameters.

    Engineer question - answer before running code

    Which bugs can the compiler prevent in the good version, and which business rules still require runtime validation?

    Mini refactor task

    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.

    Sign in to mark lessons done and keep your place in the course.Sign in