Source: http://blogs.tedneward.com/patterns/Builder-CSharp/
class GuardedFluentBuilder { private Product product; public FluentBuilder Begin(string name) { product = new Product(name); return this; } public FluentBuilder Engine { get { product.Parts.Add ("Engine"); return this; } } public FluentBuilder SteeringWheel { get { product.Parts.Add("Steering Wheel"); return this; } } public FluentBuilder Tire() { product.Parts.Add("Tire"); return this; } public Product Build() { if (product.Parts.Length < 4) throw new Exception("Product doesn't have enough parts to it yet"); if (product.Parts.Contains("Engine") == false) throw new Exception("Product must have an Engine"); if (product.Parts.Find((str) => str == "Tire").Length < 2) throw new Exception("Product must have at least 2 Tires"); return product; } } var builder = new FluentBuilder(); product = builder.Begin("MyProduct") .Engine .SteeringWheel .Tire() .Tire() .Build();
89700cookie-checkC# Builder Pattern