Skip to content
Katabench
Try free

Free C# design-principles practice in the browser

C# SOLID principles exercises that grade the design

Refactor working C# until the code is easier to change for a reason you can prove. Behavior tests protect the feature while 244 executable rules grade abstractions, immutability, interfaces, dependencies, and pattern structure across 28 focused exercises.

All exercises are available on every plan. No project setup or local SDK.

Katabench results after a Run of the Ask for the Interface kata, with the OrderNotifier field typed as IMessenger but still set to new SmtpEmailer(). The verdict, Failed: design rule, says all 2 sample cases passed and 4 / 5 design rules pass. The rules that an IMessenger interface exists and that OrderNotifier depends on it pass, while the red rule, OrderNotifier must not reference the concrete SmtpEmailer, reports that Notifications.OrderNotifier depends on Notifications.SmtpEmailer.
A Run on Ask for the Interface with the field retyped to IMessenger: the samples pass, and the rule against referencing SmtpEmailer still fails.
design exercises
28
executable rules
244
design exercises on the Free plan
28

Beyond naming the acronym

Make the principle survive a workaround

Reading that high-level code should depend on abstractions is easy. The useful judgment is recognizing when an interface still leaks a vendor DTO, a class accepts the interface but constructs the concrete anyway, or a service locator merely moves behind a readonly field.

Each kata starts with working code and one plausible design failure. You refactor several files, keep 122 catalog behavior tests green, and satisfy authored rules that inspect the submitted type structure. A cosmetic rename cannot clear a dependency rule.

Start with the dependency inversion guide, the value-object guide, or the Result pattern guide, then turn the idea into a graded refactor.

Your refactor

IPricingStrategy.cs

PricingCalculator.cs

CompositionRoot.cs

Working code is supplied. You change the design without changing the behavior.

Behavior suite

The feature still calculates every price correctly

PASS

Executable design rules

depends on abstraction

PricingCalculator → IPricingStrategy

✓ HOLDS

no concrete dependency

PricingCalculator ↛ PremiumPricing

✓ HOLDS

sealed policy owner

PricingCalculator is sealed

✓ HOLDS
Tests protect what the code does. Structural rules prove the submitted design has the intended shape.

SOLID in C#

One exercise for each principle

Looking for one SOLID principle in particular? Start with its exercise here. The families below group the rest by the design decision they force.

  • Single Responsibility Medium

    One Reason to Change

    PaymentImporter reads bank-statement lines, decides duplicates against its own dictionary, and words the summary, so a new statement column, a move to a database, and new finance wording all edit the same method. Split it along those reasons to change, and let the store decide what counts as a duplicate at the moment it stores a payment.

  • Open/Closed Hard

    Strategy Over the Switch

    A price calculator switches on a plan-name string and news up each concrete strategy by hand. Extracting an IPricingStrategy looks like the fix, but a tempting in-class factory keeps the concretes wired into the calculator. Make the calculator depend only on the abstraction and let the composition root choose the plan.

  • Liskov Substitution Hard

    Make the Membership Contract Honest

    An abstract Membership promises renewal even though lifetime memberships cannot renew, forcing RenewalService to recognize a concrete subtype before it can safely use the base contract. Split shared membership identity from the renewable capability, remove the lying inheritance, and make the service depend only on what it can honestly call.

  • Interface Segregation Medium

    Shrink the Machine Interface

    One fat IMachine interface forces the inkjet printer to fake a Scan it cannot perform, and the print queue to demand a scanner it never uses. Split the roles into IPrinter and IScanner. Keeping IMachine around as a composed sub-interface leaves the inkjet transitively welded to scanning and two rules red.

  • Dependency Inversion Easy

    Ask for the Interface

    An order notifier news up its own SmtpEmailer and types the field as the concrete class, even though an IMessenger abstraction sits right next to it. Make the notifier ask for the interface in its constructor. Typing the field as IMessenger while still newing the concrete inside is not the fix.

Four object-design skill families

Practice the pressure that makes each principle useful

The exercises are organized by the design decision they force, not by flashcard letters. That keeps Result models, honest capability contracts, Observer, Strategy, Decorator, value objects, and SOLID principles attached to the problems they actually solve.

Encapsulation, explicit outcomes, and invariants

9 exercises

Make state ownership, resource lifetimes, and expected outcomes explicit, close mutation leaks, keep snapshots opaque, and encode invariants in types instead of relying on callers to behave.

Single responsibility, dependency inversion, and clean ports

6 exercises

Split a class along its reasons to change, depend on abstractions owned by the policy side, remove the ones nothing varies behind, expose hidden dependencies, and keep vendor types outside application-facing contracts.

Substitutability, interface segregation, and low coupling

4 exercises

Keep subtype contracts honest, shrink roles to what each consumer actually needs, separate queries from commands, and replace long object-navigation chains with intention-revealing boundaries.

Composition and behavioral patterns

9 exercises

Use observers, policies, strategies, null objects, decorators, composites, state objects, undoable commands, and visitors where fixed collaborators, inheritance, switches, type tests, nullable branches, boolean flags, and tagged trees resist change.

28 free C# SOLID and design-pattern exercises

Refactor every design in the browser

Start with one concrete dependency or train wreck, then move into explicit outcomes, honest subtype contracts, value objects, composition, Observer, Strategy, Null Object, and Decorator. Every card opens the actual kata.

9 focused refactors

Encapsulation, explicit outcomes, and invariants

  1. Medium 4 tests + 10 rules

    History Keeps a Checkpoint, Not Editor Internals

    Give history immutable checkpoints it can restore repeatedly without seeing the editor's internals.

    Refactor this design: History Keeps a Checkpoint, Not Editor Internals Read the brief: History Keeps a Checkpoint, Not Editor Internals
  2. Medium 3 tests + 5 rules

    Freeze the Money

    Turn a mutable Money-shaped DTO into a structurally immutable value object.

    Refactor this design: Freeze the Money Read the brief: Freeze the Money
  3. Medium 6 tests + 4 rules

    An OrderId Is Not a CustomerId

    Give order and customer identifiers their own always-valid value types so a swapped argument stops compiling.

    Refactor this design: An OrderId Is Not a CustomerId Read the brief: An OrderId Is Not a CustomerId
  4. Medium 3 tests + 3 rules

    Kill the Global Counter

    Give mutable state one explicit owner and remove a process-wide static coupling.

    Refactor this design: Kill the Global Counter Read the brief: Kill the Global Counter
  5. Medium 5 tests + 10 rules

    Make Failure a Result

    Replace a hidden throw/catch protocol with one sealed, immutable enrollment outcome.

    Refactor this design: Make Failure a Result Read the brief: Make Failure a Result
  6. Medium 6 tests + 13 rules

    Make Illegal States Unrepresentable

    Model each parcel stage as an immutable type that carries only its own data and legal moves.

    Refactor this design: Make Illegal States Unrepresentable Read the brief: Make Illegal States Unrepresentable
  7. Hard 3 tests + 6 rules

    Make It a Value Object

    Freeze a two-phase Reservation and stop its guest collection escaping through the API.

    Refactor this design: Make It a Value Object Read the brief: Make It a Value Object
  8. Medium 4 tests + 9 rules

    Own the Lease Until the Work Ends

    Acquire and deterministically dispose a lease in one scope, exception paths included, from any lease factory.

    Refactor this design: Own the Lease Until the Work Ends Read the brief: Own the Lease Until the Work Ends
  9. Hard 3 tests + 4 rules

    Seal the Aggregate

    Protect an Order invariant by sealing mutation paths and its backing collection.

    Refactor this design: Seal the Aggregate Read the brief: Seal the Aggregate

6 focused refactors

Single responsibility, dependency inversion, and clean ports

  1. Easy 3 tests + 5 rules

    Ask for the Interface

    Replace a hidden concrete dependency with an explicit abstraction-owned constructor seam.

    Refactor this design: Ask for the Interface Read the brief: Ask for the Interface
  2. Medium 7 tests + 7 rules

    Four Layers for One Formula

    Remove the abstractions nothing varies behind and keep the one that does, without changing a single report card.

    Refactor this design: Four Layers for One Formula Read the brief: Four Layers for One Formula
  3. Hard 3 tests + 5 rules

    The Leaky Payment Port

    Build a vendor-neutral payment port instead of leaking Stripe types through an interface.

    Refactor this design: The Leaky Payment Port Read the brief: The Leaky Payment Port
  4. Medium 5 tests + 13 rules

    One Reason to Change

    Split a payment importer so statement parsing, storage, and summary wording each change in one place.

    Refactor this design: One Reason to Change Read the brief: One Reason to Change
  5. Hard 3 tests + 4 rules

    Program to the Abstraction

    Make every payment method a conventionally named drop-in over one real abstraction.

    Refactor this design: Program to the Abstraction Read the brief: Program to the Abstraction
  6. Medium 4 tests + 5 rules

    Retire the Service Locator

    Expose a quote calculator's hidden global dependency through constructor injection.

    Refactor this design: Retire the Service Locator Read the brief: Retire the Service Locator

4 focused refactors

Substitutability, interface segregation, and low coupling

  1. Hard 5 tests + 18 rules

    Make the Membership Contract Honest

    Split shared membership identity from renewal so lifetime members inherit no lying method.

    Refactor this design: Make the Membership Contract Honest Read the brief: Make the Membership Contract Honest
  2. Easy 4 tests + 7 rules

    A Preview Must Not Consume Work

    Hand queue observers a read-only preview and keep consumption behind an explicit command.

    Refactor this design: A Preview Must Not Consume Work Read the brief: A Preview Must Not Consume Work
  3. Medium 3 tests + 8 rules

    Shrink the Machine Interface

    Split a fat machine contract so printers and consumers depend only on roles they use.

    Refactor this design: Shrink the Machine Interface Read the brief: Shrink the Machine Interface
  4. Easy 3 tests + 5 rules

    Stop the Train Wreck

    Replace a multi-object navigation chain with one intention-revealing neighbor call.

    Refactor this design: Stop the Train Wreck Read the brief: Stop the Train Wreck

9 focused refactors

Composition and behavioral patterns

  1. Hard 6 tests + 23 rules

    A Rule Tree Can Contain Any Rule

    Replace a tagged-tree evaluator with recursive rule objects that keep child order and short-circuit behavior.

    Refactor this design: A Rule Tree Can Contain Any Rule Read the brief: A Rule Tree Can Contain Any Rule
  2. Hard 6 tests + 11 rules

    Compose the Fees

    Flatten a fee hierarchy into a sealed account composed with interchangeable policies.

    Refactor this design: Compose the Fees Read the brief: Compose the Fees
  3. Hard 5 tests + 10 rules

    Notify Every Observer

    Broadcast build completion through an open observer collection and prove every subscriber runs.

    Refactor this design: Notify Every Observer Read the brief: Notify Every Observer
  4. Medium 3 tests + 5 rules

    Null Object: No Branching

    Replace repeated null checks with a substitutable NoDiscount supplied at composition.

    Refactor this design: Null Object: No Branching Read the brief: Null Object: No Branching
  5. Hard 5 tests + 5 rules

    Strategy Over the Switch

    Move plan-specific pricing behind strategies chosen outside the calculator.

    Refactor this design: Strategy Over the Switch Read the brief: Strategy Over the Switch
  6. Hard 4 tests + 14 rules

    Adopt the State That Comes Back

    Move lifecycle decisions into state objects and adopt every successor they return across a whole sequence.

    Refactor this design: Adopt the State That Comes Back Read the brief: Adopt the State That Comes Back
  7. Medium 6 tests + 11 rules

    Undo What Actually Happened

    Turn stock adjustments into commands that undo what really happened behind a history that knows no stock.

    Refactor this design: Undo What Actually Happened Read the brief: Undo What Actually Happened
  8. Hard 5 tests + 10 rules

    Visit Every Invoice Line

    Replace type switches and their silent defaults with visitors, so a new invoice line kind fails to compile.

    Refactor this design: Visit Every Invoice Line Read the brief: Visit Every Invoice Line
  9. Hard 5 tests + 14 rules

    Wrap It, Don't Flag It

    Replace multiplying boolean branches with stackable decorators over one announcer role.

    Refactor this design: Wrap It, Don't Flag It Read the brief: Wrap It, Don't Flag It

The design-practice loop

Preserve the feature, change the reason it is easy to extend

1

Find the design pressure

Read working C# with one specific rigidity: a concrete dependency, fat interface, mutation leak, switch, flag, or global hiding the real seam.

2

Refactor the shape

Edit the multi-file project in the browser. Introduce the smallest abstraction, policy, wrapper, or invariant boundary that resolves the pressure.

3

Prove behavior and design

Submit against behavior tests and executable structural rules. The feature must still work, and the dependency or type shape must genuinely change.

The structural verdict is separate from correctness. Read how Katabench grades design rules and why a compiling implementation can still be the wrong shape.

C# SOLID principles exercise FAQ

Before you move the first dependency

What are C# SOLID principles exercises?

They are small working C# systems with one design pressure already present. Instead of answering a quiz about an acronym, you refactor the code and receive separate verdicts for preserved behavior and structural design rules such as abstraction use, interface segregation, immutability, sealing, and dependency direction.

Do these exercises cover all five SOLID principles?

The strongest direct coverage is Open/Closed, Liskov Substitution, Interface Segregation, and Dependency Inversion, and one exercise splits a class along its reasons to change for Single Responsibility. Related exercises train state ownership, explicit outcomes, composition over inheritance, encapsulation, and value objects. It is a focused practice catalog, not a five-chapter theory course that forces every kata under one letter.

Which C# design patterns can I practice?

The catalog includes Strategy, Decorator, Observer, Composite, State, Memento, Command with undo, Visitor, Null Object, Value Object, strongly typed identifiers, one type per lifecycle stage, explicit Result models, command-query separation, composition roots, constructor injection, anti-corruption ports, composition over inheritance, and removing speculative abstractions. Each pattern appears as a response to a concrete design problem rather than an isolated template to copy.

Are all 28 design-principles exercises free?

Yes. The complete Design Principles track is currently available on all plans. Daily graded-submission limits still depend on your plan, but none of these exercises is locked behind a specialized-track entitlement.

Do I need Visual Studio or the .NET SDK installed?

No. The multi-file editor, compiler, behavior tests, and design-rule grader run from the browser in disposable server-side sandboxes.

How are design-principles exercises different from architecture exercises?

Design-principles exercises focus on the internal shape and collaborators of a few types: immutability, interfaces, roles, dependencies, and patterns. Architecture exercises operate at the larger namespace, layer, module, and dependency-graph level.

Looking for broader practice? Browse all C# coding challenges across algorithms, data access, refactoring, architecture, security, testing, and design, or see every design principles kata in the catalog.

Make one dependency prove the principle

Start with Ask for the Interface: working notification code, one hidden concrete dependency, and five structural rules that reject the cosmetic fixes.

Try dependency inversion free