Tutorials Design Patterns in C#

State Pattern — Complete Guide

State Pattern — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of Design Patterns in C# on Toolliyo Academy.

On this page

Design Patterns in C# · Lesson 20 of 69

State Pattern

GoF Core ✓EnterpriseCloud & Craft

Enterprise · 2 — App patterns · ~6 min · Module 3: Behavioral Design Patterns

What is this?

State pattern moves state-specific behavior into state objects so the context delegates instead of giant switch statements.

Why should you care?

ShopNest orders (Pending → Paid → Shipped → Cancelled) have different allowed actions per state.

See it live — copy this example

Paste into a C# console or class library project and run dotnet run.

public interface IOrderState { void Pay(Order o); void Ship(Order o); }
public sealed class Order
{
    public IOrderState State { get; set; } = new PendingState();
    public void Pay() => State.Pay(this);
    public void Ship() => State.Ship(this);
}
public sealed class PendingState : IOrderState
{
    public void Pay(Order o) { Console.WriteLine("paid"); o.State = new PaidState(); }
    public void Ship(Order o) => Console.WriteLine("cannot ship yet");
}
public sealed class PaidState : IOrderState
{
    public void Pay(Order o) => Console.WriteLine("already paid");
    public void Ship(Order o) { Console.WriteLine("shipped"); o.State = new ShippedState(); }
}
public sealed class ShippedState : IOrderState
{
    public void Pay(Order o) => Console.WriteLine("noop");
    public void Ship(Order o) => Console.WriteLine("already shipped");
}

var order = new Order();
order.Pay();
order.Ship();

Run Example »

Edit the code below and click Run to see the result in Toolliyo’s live editor.

Code
Result

What happened?

  • PendingState allows Pay; PaidState allows Ship.
  • Transitions assign a new state object.
  • Illegal calls become no-ops or errors inside the state.

Practice next

  1. Pay then Ship successfully.
  2. Try Ship first and see refusal.
  3. Add CancelledState.
  4. Throw on illegal Ship from Pending.
  5. Log every transition.

Remember

Behavior per state object. Clear transitions. Replace mega-switches.

ShopNest order lifecycle

Domain Order delegates to state objects.

Outcome: Illegal transitions blocked in one place.

Interview prep for this lesson

Practice these questions aloud after reading—each links to a full structured answer.

Mid PDF Detailed
State Interface (ITrafficLightState):?
Short answer: The ITrafficLightState interface defines a method (Change) that allows the state to transition to another state. The method accepts a TrafficLight object as a parameter to facilitate the state change. publi…
Mid PDF Detailed
Finite State Machines (FSM):?
Short answer: When you have an object that can be in multiple predefined states and its behavior depends on the current state (e.g., traffic lights, order processing systems). Say this in the interview Define — one clear…
Mid PDF Detailed
State Interface:?
Short answer: The ITrafficLightState interface defines a method (Change) that allows the traffic light to transition between states (Red, Green, Yellow). Real-world example (ShopNest) Use a GoF pattern in ShopNest only w…
Mid PDF Detailed
Global State:?
Short answer: The Singleton can act as a global variable, which may make it harder to track state changes and can lead to issues with dependencies. Real-world example (ShopNest) Use a GoF pattern in ShopNest only when it…
Mid PDF Detailed
Abstract Factory (LoggerFactory):?
Short answer: The LoggerFactory class defines a factory method CreateLogger that returns an ILogger object. This is a generic interface for creating various logger types without specifying the concrete class directly. Re…
Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

Design Patterns in C#
Course syllabus

Design Patterns in C# Tutorial

Module 1: Creational Design Patterns
Module 2: Structural Design Patterns
Module 3: Behavioral Design Patterns
Module 4: Enterprise Design Patterns
Module 5: Modern Enterprise Patterns
Module 6: Microservices & Cloud Patterns
Module 7: ASP.NET Core Architecture Patterns
Module 8: Interview & System Design
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details