Β© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#β€’3y agoβ€’
57 replies
Stroniax

Builder Pattern as Ref Struct

Hey all, I'm trying to make a source-generated builder which I want to be a struct. I'm running into a problem when trying to chain the entire build, but if I capture the builder in a variable first I can chain it as much as I like. Wondering if there's any language feature/etc that would help me out here? What I'd like is to use the same builder struct instance and not have to copy it with each chained method.

public struct MyBuilder {
  public required int Id { get; set; }
  public string? Name { get; set; }
  public My Build() => new(this);
}
public static class MyBuilderExtensions {
  public ref MyBuilder WithId(this ref MyBuilder b, int id) {
    b.Id = id;
    return ref b;
  }
  public ref MyBuilder WithName(this ref MyBuilder b, string? name) {
    b.Name = name;
    return ref b;
  }
}
public static class Usage {
  public static My Works() {
    var builder = new MyBuilder() { Id = 1 };
    return builder.WithName("Test").Build();
  }
  public static My DoesNotWork() {
    // CS1510: A ref or out value must be an assignable variable
    return new MyBuilder() { Id = 1 }.WithName("Test").Build();
  }
}
public partial class My
{
  public required int Id { get => throw null; init => throw null; }
  public string Name { get => throw null; init => throw null; }
  // only if set by builder/init
  public bool TryGetName(out string name) => throw null;;
  [SetsRequiredMembers]
  public My(int id) => throw null;
  [SetsRequiredMembers]
  public My(MyBuilder b) => throw null;
}
public struct MyBuilder {
  public required int Id { get; set; }
  public string? Name { get; set; }
  public My Build() => new(this);
}
public static class MyBuilderExtensions {
  public ref MyBuilder WithId(this ref MyBuilder b, int id) {
    b.Id = id;
    return ref b;
  }
  public ref MyBuilder WithName(this ref MyBuilder b, string? name) {
    b.Name = name;
    return ref b;
  }
}
public static class Usage {
  public static My Works() {
    var builder = new MyBuilder() { Id = 1 };
    return builder.WithName("Test").Build();
  }
  public static My DoesNotWork() {
    // CS1510: A ref or out value must be an assignable variable
    return new MyBuilder() { Id = 1 }.WithName("Test").Build();
  }
}
public partial class My
{
  public required int Id { get => throw null; init => throw null; }
  public string Name { get => throw null; init => throw null; }
  // only if set by builder/init
  public bool TryGetName(out string name) => throw null;;
  [SetsRequiredMembers]
  public My(int id) => throw null;
  [SetsRequiredMembers]
  public My(MyBuilder b) => throw null;
}


EDIT:
The problem occurs because the extension method operates on a
ref
ref
of the instance, so I must have a field/variable declared beforehand of the type. This means I can't just call
new().Extension()
new().Extension()
, as the result of
new()
new()
must be assigned to something before it can be used. That is what I am trying to resolve.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements
Next page

Similar Threads

difference between `struct` and `ref struct`
C#CC# / help
12mo ago
Builder pattern question
C#CC# / help
4y ago
❔ StructureMap.StructureMapException: StructureMap Exception Code: 207
C#CC# / help
3y ago
❔ Benefit of ref structs
C#CC# / help
3y ago