Step Builder Design Pattern In C#

In the last blog, we explored how immutability strengthens the traditional Builder Pattern. By making the User class immutable (no setters, only read‑only properties), we ensured that once an object is created, its state cannot change.

We then introduced the Immutable Builder, which temporarily collects values and produces the immutable object only when Build() is called. This approach gives us:

  • Thread safety – immutable objects are naturally safe across threads
  • No accidental modifications – values stay fixed once set
  • Predictable behavior – ideal for configurations, DTOs, and fixed states

We also discussed when to use it (fixed state, functional style, preventing accidental changes) and when not to (frequent updates, performance‑critical scenarios).

Before continuing in this blog I recomment you to read the Immutable Builder Design Pattern In C#. To read the blog click here

Let’s explore Step Builder Design Pattern In C#

Sometimes object creation must follow a specific order. 

For example: 

A user must have a Name and Email before anything else. 

The Step Builder Pattern enforces this rule at compile time. 

In short

The Step Builder pattern in C# is an extension of the standard Builder Design Pattern that enforces the correct order of construction steps at compile-time. This prevents the client code from creating an incomplete object or skipping required configuration steps, thus eliminating potential runtime errors. 

Step Builder Interface

public interface IUserNameStep 
{ 
   IUserEmailStep SetName(string name); 
  // Since after setting name above EmailStep interfece is specified so before setting the name, email or other things can not be set
} 
 
public interface IUserEmailStep 
{ 
   IUserOptionalStep SetEmail(string email); 
// Since after setting email above OptionalStep interfece is specified so before setting the email phone, address and age can not be set
} 
 
public interface IUserOptionalStep 
{ 
   IUserOptionalStep SetPhone(string phone); 
   IUserOptionalStep SetAddress(string address); 
   IUserOptionalStep SetAge(int age); 
   User Build(); 
}

Concrete Step Builder

public class StepUserBuilder : 
   IUserNameStep, 
   IUserEmailStep, 
   IUserOptionalStep 
{ 
   private User _user = new User(); 
 
   public IUserEmailStep SetName(string name) 
   { 
       _user.Name = name; 
       return this; 
   } 
 
   public IUserOptionalStep SetEmail(string email) 
   { 
       _user.Email = email; 
       return this; 
   } 
 
   public IUserOptionalStep SetPhone(string phone) 
   { 
       _user.Phone = phone; 
       return this; 
   } 
 
   public IUserOptionalStep SetAddress(string address) 
   { 
       _user.Address = address; 
       return this; 
   } 
 
   public IUserOptionalStep SetAge(int age) 
   { 
       _user.Age = age; 
       return this; 
   } 
 
   public User Build() 
   { 
       return _user; 
   } 
} 

Usage: 

Below Code will not work as step for email is before address

        User user = new StepUserBuilder()
            .SetName("Avinash")            
            .SetAddress("123 AbC street") // not working because email should set first
            .SetEmail("abc@xyz.com")
            .SetPhone("23456666")
            .SetAge(35)
            .Build();

Below code will also not work as step for name should be before email

 User user = new StepUserBuilder()           
     .SetEmail("abc@xyz.com") 
      .SetName("Avinash") // not worked
     .SetAddress("123 AbC street")
     .SetPhone("23456666")
     .SetAge(35)
     .Build();

Correct steps will be

 User user = new StepUserBuilder()
     .SetName("Avinash")
     .SetEmail("abc@xyz.com")
     .SetAddress("123 AbC street")
     .SetPhone("23456666")
     .SetAge(35)
     .Build();

This guarantees: 

  • Name must be set first 
  • Email must be set second 

When to Use Step Builder

Use when:

  • Object creation has mandatory steps
  • Order matters
  • You want compile-time safety

Examples:

  • Authentication requests
  • Payment requests
  • Complex API requests

That’s it for the blog, In the next blog we will explore Faceted Builder Design Pattern

Read more here

Recommended Topics

Popular Tags

.net .Net Core .NET Developers .NET Development Future .NET Productivity .NET programming agentic ai AI Agents AI Tools .NET app.Map Azure AI Boilerplate Builder Design Pattern in C# C# C# AI C# Design Patterns C# Programming circuit breaker pattern Code Assistants Coding Coding best practices Coding in AI Creational Design Patterns Design Patterns Design Patterns in C# dotnet core resilience Factory Design Patterns in C# Fluent Builder Design Pattern Generative AI Immutable Builder Design Pattern In C# Knowledge Lightweight API LLMs .NET Machine Learning .NET MapGet Microservices microservices resilience ML.NET Motivational polly v8 resilience architecture REST API retry pattern Semantic Kernel Singleton Design Pattern in C# Software Architecture Step Builder Design Pattern In C# The Avinash Joshi TheAvinashJoshi Top 5 AI tools trending coding methods vibe coding Visual Studio AI Web API Web Development