0
0
CsharpConceptBeginner · 4 min read

Builder Pattern in C#: What It Is and How to Use It

The builder pattern in C# is a design pattern that helps create complex objects step-by-step using a separate builder class. It separates the construction of an object from its representation, making the creation process clearer and more flexible.
⚙️

How It Works

Imagine you want to build a custom sandwich. Instead of making it all at once, you add ingredients one by one: bread, lettuce, cheese, and so on. The builder pattern works the same way for objects in C#. It breaks down the creation of a complex object into smaller steps, each handled by a method in a builder class.

This pattern separates the process of building an object from the final object itself. You use a builder to set up parts of the object, and then you get the finished product. This makes your code easier to read and change, especially when objects have many optional parts or configurations.

💻

Example

This example shows how to build a Car object step-by-step using a CarBuilder. You can set the color, engine type, and number of doors separately before getting the final car.

csharp
public class Car
{
    public string Color { get; set; }
    public string Engine { get; set; }
    public int Doors { get; set; }

    public override string ToString() => $"Car with {Color} color, {Engine} engine, and {Doors} doors.";
}

public class CarBuilder
{
    private Car _car = new Car();

    public CarBuilder SetColor(string color)
    {
        _car.Color = color;
        return this;
    }

    public CarBuilder SetEngine(string engine)
    {
        _car.Engine = engine;
        return this;
    }

    public CarBuilder SetDoors(int doors)
    {
        _car.Doors = doors;
        return this;
    }

    public Car Build() => _car;
}

class Program
{
    static void Main()
    {
        var builder = new CarBuilder();
        Car car = builder.SetColor("Red")
                         .SetEngine("V8")
                         .SetDoors(2)
                         .Build();
        System.Console.WriteLine(car);
    }
}
Output
Car with Red color, V8 engine, and 2 doors.
🎯

When to Use

Use the builder pattern when you need to create complex objects with many parts or options, especially if some parts are optional or can be combined in different ways. It helps keep your code clean and easy to maintain.

For example, it is useful when building user interface elements, configuring database queries, or creating objects with many settings like cars, houses, or reports. It also helps when you want to avoid constructors with many parameters, which can be confusing.

Key Points

  • The builder pattern separates object construction from its representation.
  • It builds objects step-by-step using a builder class.
  • It improves code readability and flexibility for complex objects.
  • It avoids constructors with many parameters.
  • It is useful for objects with optional or varying parts.

Key Takeaways

The builder pattern creates complex objects step-by-step using a separate builder class.
It makes object creation clearer and easier to manage than large constructors.
Use it when objects have many optional or configurable parts.
It improves code readability and flexibility.
The final object is returned only after all parts are set.