As lead of a C# statistics package targeting netstandard2.0 and net8.0, would you replace its per-type Mean overloads with one INumber<T> generic method?
answer
- which targets can see INumber<T>
- compiled consumers bind to signatures
- constraints are public contract
- result and accumulator types
- multi-target and forward
basics
~20 sNot as a replacement. Keep the per-type overloads, which netstandard2.0 needs and compiled consumers bind to, and add a generic Mean on the net8.0 target only, with the narrowest constraints and an explicit result type, forwarding the old overloads to it.
solid answer
~40 sI would add, not replace. Generic math needs .NET 7 or later: the interfaces do not exist in netstandard2.0, and static abstract members need runtime support (CS8919 otherwise), so that target keeps its overloads. Removing public overloads also breaks already-compiled consumers with `MissingMethodException`, so on net8.0 I keep `Mean(ReadOnlySpan<int>)` and its siblings and make them forward to one private generic core, which is how `Enumerable.Sum` is built in .NET 10. I would then expose a public generic `Mean<T, TResult>` only if consumers bring their own numeric types. Its constraints become contract, so I start from the narrowest set the algorithm uses rather than `INumber<T>`: loosening later is safe for callers, tightening breaks them. The result type must be explicit, because `Mean<int>` would otherwise divide in `int` and truncate.
go deeper
Recall that INumber<T> and generic math need .NET 7 or later, so older targets cannot use them.
Explain why removing a public overload breaks compiled consumers and why Mean<int> in generic code divides in int.
Show the multi-targeting plan: overloads kept for netstandard2.0, a private generic core on net8.0, explicit result and accumulator types.
Weigh one generic entry point against API stability: who your consumers are, which targets they run, and which constraints you are willing to promise for years.
## What the decision is really about Generic math turns five near-identical method bodies into one, and the temptation is to delete the overloads and ship `Mean<T>(ReadOnlySpan<T> values) where T : INumber<T>`. For an application's internal code that is often fine. For a **published package** it is a decision about who can still compile against you, which binaries keep running, and what contract you are promising for years. There is no single right answer; there are constraints that do not move and choices that depend on your consumers. ## The constraints that do not move - **Target frameworks.** The `INumber<T>` family ships with .NET 7 and later. netstandard2.0 has no such interfaces, and static abstract interface members need runtime support: declaring one for an older runtime gives CS8919, "Target runtime doesn't support static abstract members in interfaces." A package that serves netstandard2.0 consumers keeps non-generic code on that target. - **Binary compatibility.** An app compiled against `Mean(int[])` references that exact method. Remove it and the app throws `MissingMethodException` at run time, before anyone recompiles. - **Semantics per type.** Generic code runs `T`'s own operators, so `Mean<int>` of 1, 2, 3 and 4 is 2. Per-type overloads could return `double` for integer input; a generic API must expose that choice as a type parameter. - **Accumulator overflow.** Summing `byte` or `short` in `T` wraps. The BCL faced this too: `Enumerable.Sum(IEnumerable<float>)` keeps its public per-type signature and delegates to a private generic helper that accumulates in `double`. ## The options | Option | Gains | Costs | |---|---|---| | Keep per-type overloads only | works on every target; no new contract | duplicated bodies; no `Half`, `Int128` or consumer types | | Replace them with `Mean<T>` | one body; any numeric type | breaks netstandard2.0 and compiled consumers; integer semantics change | | Keep overloads, forward to a generic core, add a public generic method on net8.0 | one implementation; old binaries keep working; new types supported | multi-targeting, `#if NET7_0_OR_GREATER`, and a new public contract to maintain | ## How I would ship it 1. Multi-target `netstandard2.0` and `net8.0`. 2. On `net8.0`, move the arithmetic into one private generic core under `#if NET7_0_OR_GREATER`, and make every existing public overload forward to it, so behaviour and signatures stay identical. 3. Add a public `Mean<T, TResult>` only when consumers ask for types the overloads do not cover, such as `Half`, `Int128` or their own `Money` struct. 4. Choose the narrowest constraints the algorithm needs, such as addition, an additive identity and division for `TResult`, rather than `INumber<T>`, which demands about sixty members from any custom type. 5. Make the result type explicit and document overflow: say whether the sum is checked, and in which type it accumulates. 6. Run the same tests against both targets and check the public API surface in CI, so a removed overload is caught before release. ## Why constraints deserve extra care A `where` clause on a public method is part of its contract: - **Loosening** it, say from `INumber<T>` to a few operator interfaces, keeps every existing caller compiling. - **Tightening** it later breaks any caller whose type argument no longer qualifies, with a compile error at their call site. - Starting narrow is therefore the cheaper mistake; you can always widen later. On performance, the JIT compiles a separate copy of a generic method for each value-type argument, so `T.Zero` and `+` become direct calls without boxing; measure before claiming a hand-written overload is faster. ## When the answer flips - A library that targets only .NET 8 or later and has not yet shipped a stable API can go generic-first. - A hot path with hand-tuned per-type code may keep those overloads and use the generic core only for the rest. - If no consumer has ever asked for another numeric type, the forwarding refactor may still pay for itself in maintenance, but the public generic method can wait. ## Communicating the change The forwarding refactor is invisible to consumers, but a new public generic method is not. Release notes should say which types the generic method accepts, which result type it returns for integer input, and whether overflow throws. A consumer reading `Mean<T, TResult>` for the first time needs to know that `Mean<int, int>` truncates and `Mean<int, double>` does not. A strong answer names the target-framework limit, the binary-compatibility rule, the integer-division semantics and the constraint-as-contract point, then lands on add-and-forward rather than replace.
- Why not constrain the new method to INumber<T>, since every built-in numeric type implements it?Because it demands far more than a mean needs. A custom `Money` or units-of-measure struct would have to implement about sixty members, including parsing, comparison and `Money * Money`, before it could call `Mean`. The narrow operator interfaces let such types join, and widening later stays possible, while tightening would break callers.
- How do you keep the netstandard2.0 and net8.0 builds from diverging in behaviour?Keep one test suite and run it against both target frameworks, with the same inputs and expected outputs per type. On net8.0 the overloads forward to the generic core; on netstandard2.0 they keep their hand-written bodies, and any behaviour difference, such as the accumulator type or overflow handling, shows up as a failing test instead of a bug report.
- Will the generic version be slower than the hand-written overloads?Usually not for value types: the JIT compiles a dedicated copy of the generic method for each value-type argument, so the static abstract calls become direct calls with no boxing. Hand-tuned code, such as explicit vectorisation, can still win on hot paths. Measure with a benchmark before deciding, rather than assuming either way.
- Delete the overloads; generic math is source-compatible, so nobody breaks.
- netstandard2.0 targets can use INumber<T> once LangVersion is set to 11.
- INumber<T> covers every numeric type a consumer might bring.
- Mean<int> naturally returns a fractional result in generic code.
- Tightening a public generic constraint later is a non-breaking change.