Why I Chose CRTP for My C# Result Pattern Library
I’ve been working on a C# Result Pattern library called REslava.Result—zero dependencies, functional, and designed for fluent APIs. But I kept hitting a wall: type information loss in method chains.
csharp
CopyError error = new Error("Something went wrong")
.WithMessage("Updated message") // Returns Reason, not Error
.WithTag("Code", 404); // Can't call Error-specific methods
Then I discovered CRTP (Curiously Recurring Template Pattern). It’s not magic—just smart C#—and it preserved the derived type through the entire chain:
csharp
CopyError error = new Error("Something went wrong")
.WithMessage("Updated message") // Still returns Error
.WithTag("Code", 404) // Type-safe and fluent
.WithTag("Field", "Email");
This solved the core problem: type safety and developer experience in fluent APIs.
I wrote about my journey—how I learned CRTP, generics, functional patterns, and library design—and why this approach beats alternatives.
If you’re into:
- C# advanced patterns
- Functional programming in .NET
- Building reusable libraries
- Or just love solving type puzzles
I’d love to hear your thoughts. What’s your experience with CRTP or Result patterns?
Posted Jan 27, 2026, 11:43 AM UTC
04