đź”§ C#: Fix a Generic Math Operator Not Resolving for a Custom Type
Generic math (the static abstract interface members introduced for numeric interfaces like INumber<T>) requires a custom type to explicitly implement the specific operator interfaces it wants to participate in — implementing IAdditionOperators<TSelf, TOther, TResult> doesn’t automatically grant you IComparisonOperators or IMultiplyOperators, each has to be declared separately, and the compiler’s error when a generic method constrained to a broader interface like INumber<T> is called with a type implementing only a subset of its required operators is a fairly generic “does not implement interface member” message that doesn’t clearly point at which specific operator is missing, especially once several numeric interfaces are layered together.
🔎 The Problem
public readonly struct Money :
IAdditionOperators<Money, Money, Money>,
ISubtractionOperators<Money, Money, Money>
// Missing: IMultiplyOperators, IComparisonOperators, and several
// other members INumber<T> requires - only a subset was implemented
{
public decimal Amount { get; }
public Money(decimal amount) => Amount = amount;
public static Money operator +(Money a, Money b) => new(a.Amount + b.Amount);
public static Money operator -(Money a, Money b) => new(a.Amount - b.Amount);
}
static T Sum<T>(IEnumerable<T> values) where T : INumber<T>
=> values.Aggregate(T.Zero, (acc, v) => acc + v);
Sum(new[] { new Money(10), new Money(20) });
// CS0315: 'Money' cannot satisfy the 'INumber<Money>' constraint -
// the compiler error lists the whole unsatisfied interface, not the
// specific handful of missing members out of the dozen INumber requires
âś… Fix: Implement the Narrowest Interface the Method Actually Needs
- Change the generic method’s constraint from the broad
INumber<T>down to only the specific operator interfaces it actually uses (IAdditionOperators<T, T, T>for a simple sum, for instance) — this is usually both the correct fix and the one that produces a far more specific, actionable compiler error if the custom type still doesn’t satisfy it. - If the full
INumber<T>contract is genuinely required (because the method needs comparison, parsing, or other members beyond basic arithmetic), implement the complete set of required interfaces deliberately rather than guessing — the officialINumber<T>documentation lists every member interface it composes, and working through that list methodically is faster than iterating on cryptic compiler errors one missing member at a time. - Add a small compile-time test — a generic method call against the custom type gated behind a
#ifor a dedicated test project — specifically to catch a future change that narrows which operators the type implements, since this kind of break is otherwise invisible until something downstream tries to use the type generically.
🚨 Why This Is Dangerous
- Loosely constraining a generic method to the broadest numeric interface that happens to compile against your test types can quietly lock out legitimate custom numeric types later, turning what should be a simple domain type (a Money, a Percentage, a Distance) into one that can’t participate in otherwise-generic math code without a much larger interface implementation than its actual usage ever needed.
Generic math’s operator interfaces are opt-in one at a time, by design — a type that can add and subtract hasn’t promised anything at all about whether it can be compared, multiplied, or parsed, no matter how complete that feels from the outside.




