Mid SOLID

Can you explain the Builder pattern with a real-world .NET example?

Builder separates complex object construction from its representation. For example, building

an HttpRequest with optional headers, query params, and body:

public class HttpRequestBuilder

private HttpRequestMessage _request = new HttpRequestMessage();

public HttpRequestBuilder SetMethod(HttpMethod method)

_request.Method = method;

return this;

public HttpRequestBuilder AddHeader(string key, string value)

_request.Headers.Add(key, value);

return this;

public HttpRequestMessage Build() => _request;

Allows building requests step-by-step fluently.

More from Design Patterns in C#

All questions for this course