Generic constraint for record types #9641
-
Currently, C# does not provide a way to constrain a generic type parameter to only accept ProblemWhen writing generic code that operates on records, I often want to use the public interface IFilter { }
public record MyFilter(int Page, string Query) : IFilter;
public class Consumer<TFilter> where TFilter : IFilter
{
public TFilter Clone(TFilter value)
{
// error: TFilter is not known to be a record type
return value with { };
}
} This forces developers to introduce custom ProposalIntroduce a new generic constraint that restricts a type parameter to public class Consumer<TFilter> where TFilter : record, IFilter
{
public TFilter Clone(TFilter value) => value with { };
} Benefits
Alternatives
Questions
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
See: #3859 |
Beta Was this translation helpful? Give feedback.
See: #3859