Halloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCHalloween DealsAmazon USOctober picks are moving - compare before checkoutAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk6 min

We Reinvented OOP by Making a Sandwich: A Beginner’s Guide to Classes and State in C#

Writing step-by-step instructions for a robot to make a cheese sandwich exposes hidden assumptions. Those same questions lead to C# classes, state, and methods.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Before writing any C#, try to tell a robot how to make a cheese sandwich. The exercise exposes the assumptions hidden in ordinary instructions, and those hidden assumptions are the same things a class has to name: what exists, what condition each thing is in, and which actions change that condition. Blessed Emmanuel John chidera describes this exercise in a DEV Community post dated September 19, 2026, written as a mentoring session with an apprentice named John. The method is a teaching anecdote, and the sections below follow its sequence, from vague instructions to a small C# class.

The instruction that looked finished

The exercise starts with a robot in a kitchen. It has bread, butter, a knife, a plate, and cheese, and the learner must write step-by-step instructions to assemble a cheese sandwich. In the original post, the first attempt named the objects correctly and then gave directions such as “take,” followed by ingredients combined with plus signs. To a person, that reads as a recipe. To a robot, it leaves too much open.

As an Amazon Associate I earn from qualifying purchases.

Where the first draft breaks

A robot cannot safely infer the missing details. An illustrative version of the same kind of instruction, with questions a literal machine would need answered, looks like this:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • Take what: the whole loaf, a slice, or the bag?
  • Take from where, and how is it held?
  • Is the knife clean, and is it already holding butter?
  • Does the butter go on before the cheese or after it?
  • Is the plate empty, and which slice is the base?

The plus sign is a deliberately weak model of assembly. It says that the ingredients are combined but not in what order, with which actions, or with what result. This is a criticism of the sandwich model, not of addition. In arithmetic, “2 + 3” is a complete expression. A physical assembly needs operations and an order of operations.

Naming entities, tools, a container, and operations

The revised model sorts the parts by role. The original post names the operations SCOOP, SPREAD, and LAY. Once each role is explicit, the instructions can say which thing acts and which thing is acted on.

Role Item in the exercise What the model makes explicit
Entities Bread, butter, cheese The things the task is about
Tool Knife The thing used to act on an entity
Container Plate The place where the work happens
Operations SCOOP, SPREAD, LAY The named actions that can be performed

An illustrative rewrite in that spirit reads:

  1. SCOOP butter from the tub onto the knife.
  2. SPREAD the butter from the knife onto one slice of bread on the plate.
  3. LAY one slice of cheese onto the buttered slice.

This version still does not describe a complete robotics specification. It makes inputs and actions visible, which is the purpose of the exercise.

State: the same bread in a different condition

The bridge to object-oriented thinking comes from one change. Plain bread becomes buttered bread after SPREAD runs. The slice is still the same slice. Only its condition has changed.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

That condition is the entity’s state. An operation can do one of two things: change the state, as SPREAD does, or only inspect it. The next example separates those two cases.

Commands and queries: change it or read it

The original post then uses a bank account. It begins with a $5,000 balance, deposits $1,000, withdraws $500, and then checks the balance. The check reports a value and leaves the account as it was. The post calls this distinction Command-Query Separation.

Step Kind of operation Balance after the step What the step returns
Account opened with $5,000 Setup $5,000 Nothing
Deposit $1,000 Command (changes state) $6,000 Nothing
Withdraw $500 Command (changes state) $5,500 Nothing
Check balance Query (reads state) $5,500, unchanged The current balance

The figures need a correction. The original post reports that the check returns $4,500, but the amounts it gives add up to $5,500. The table uses the stated amounts. The lesson does not depend on the arithmetic: the check reads the balance and changes nothing. Readers who copy the example should use the amounts they choose and recompute the result.

The rule described here is a convention the example follows. C# does not enforce it. A method that returns a value can also change state, and a method that changes state can also return a value. The point of the separation is to make a method’s purpose easy to read.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

From one car to a class

The post then asks how to represent 100 cars, each with a speed, a fuel level, and an engine state, without rewriting the same behavior for every car. The answer is a shared template. The template is the class. Each car built from it is an instance, with its own values.

Microsoft’s C# documentation states the idea in a sentence that fits the car example closely: “In C#, the definition of a type—a class, struct, or record—is like a blueprint that specifies what the type can do.” The sentence comes from Microsoft Learn’s C# documentation, not from a named author. The class tutorial is at Microsoft Learn: Classes and objects tutorial – C#.

The car is an entry point, not a rule. Not every noun in a program should become a class. The useful question is whether a thing has state and operations that belong together.

Mapping the model onto C#

The original post connects each idea from the sandwich and car examples to a C# member. Microsoft Learn describes the same parts: a class has members such as properties and methods, and a constructor initializes a new instance.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Model idea C# member Car example
Template for many things class Car The shared car definition
One thing built from the template new Car(...) One particular car
Condition that persists Properties such as Speed, Fuel, IsEngineRunning The car’s current speed, fuel, and engine state
Starting values Constructor A new car begins with the engine off and speed zero
Operation that changes state Methods such as StartEngine and Accelerate Turning the engine on, or increasing speed
Query that reads state Method such as CheckSpeed Reporting the speed without changing it
Rule preventing invalid state Condition inside a method Speed changes only when the engine is running

A minimal class that follows the model

The following version follows the members the original post describes. It is illustrative and has not been tested or hardened for production. In this sample, Fuel is stored but never used up.

public class Car
{
    public int Speed { get; private set; }
    public double Fuel { get; private set; }
    public bool IsEngineRunning { get; private set; }

    public Car(double fuel)
    {
        Speed = 0;
        Fuel = fuel;
        IsEngineRunning = false;
    }

    public void StartEngine()
    {
        IsEngineRunning = true;
    }

    public void Accelerate(int amount)
    {
        if (!IsEngineRunning)
            return;

        Speed += amount;
    }

    public int CheckSpeed()
    {
        return Speed;
    }
}

Each part of the sketch maps back to the model:

  • The constructor sets the starting condition of every new car.
  • StartEngine is a command: it changes IsEngineRunning.
  • Accelerate is a command with a guard. If the engine is off, the method returns before changing anything.
  • CheckSpeed is a query: it returns Speed and leaves it alone.

Two instances show how the values stay separate:

Car first = new Car(40.0);
Car second = new Car(25.5);

first.StartEngine();
first.Accelerate(20);

second.Accelerate(30);   // ignored: this engine is not running

Console.WriteLine(first.CheckSpeed());   // 20
Console.WriteLine(second.CheckSpeed());  // 0

The two cars share one definition, but each keeps its own speed and engine state.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Four questions to ask before you write code

The original post closes the exercise with prompts for planning a class before typing it:

  1. What entities exist in this problem?
  2. What state does each entity hold?
  3. For each operation, does it change state or only read it?
  4. What rules prevent invalid state, such as accelerating with the engine off?

What the exercise does and does not show

  • Evidence: The method is an author-reported teaching anecdote from a mentorship session. The original post reports no research design, measured outcomes, or comparison showing that the approach improves learning, and this article cannot add such evidence.
  • Opinion: The post’s criticism of beginner tutorials and its “tutorial hell” framing are the author’s views, not measured findings.
  • Metaphor: The plate is useful for separating an object from the context in which it is used. The post’s comparison to execution environments, memory allocation, and memory leaks is an analogy, not a technical explanation of how memory is managed.
  • Narrow scope: The sandwich covers entities, state, operations, and classes. It does not cover all of object-oriented design. Microsoft’s Object-Oriented programming (C#) tutorial outlines abstraction, encapsulation, inheritance, and polymorphism as separate topics.
  • Naming: Command-Query Separation is an established idea, and the original post presents it through its own bank example. The post does not claim the apprentice discovered it independently.

The original post is at We Reinvented OOP by Making a Sandwich (Before Writing Any Code).

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Try the exercise yourself

The original post ends with a question worth answering in your own words: how would you explain variables and assignment to a complete beginner without using the word “variable”? Try answering it as a sandwich-style list of steps, naming each thing, what it holds, and what each step does to it.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.