In C#, what problem do static abstract interface members solve, and how does a generic Mean<T> where T : INumber<T> use them?
answer
- operators are static members
- interfaces could require instance members only
- C# 11 with .NET 7
- called through the type parameter
- T.Zero and T.CreateChecked
basics
~20 sBefore C# 11, an interface could require only instance members, so generic code could not use operators or static factories on T. Static abstract members (C# 11, .NET 7) let INumber<T> require them, so Mean<T> can write T.Zero, sum += v and T.CreateChecked(count).
solid answer
~50 sOperators, constants such as zero, and parsing are `static`, and before C# 11 an interface could only make implementers supply instance members, so a generic `Mean<T>` had no way to add two `T` values: library authors wrote one overload per numeric type or passed an adder delegate. C# 11 lets an interface declare `static abstract` members, which every implementer must supply, and `static virtual` members, which carry a default body. .NET 7 used that to give the built-in numeric types `INumber<T>`, which brings `operator +`, `Zero` and `CreateChecked` through the interfaces it inherits. Inside `Mean<T>(ReadOnlySpan<T> values) where T : INumber<T>` you write `T sum = T.Zero; sum += v; return sum / T.CreateChecked(values.Length);` and each call runs `int`'s, `double`'s or `decimal`'s own implementation. Those members are reachable only through a constrained type parameter: `INumber<int>.Zero` is error CS8926.
code
csharp · 16 linesusing System.Globalization;
using System.Numerics;
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
Console.WriteLine(Mean<int>([1, 2, 3, 4])); // 2: int division
Console.WriteLine(Mean<double>([1, 2, 3, 4])); // 2.5
Console.WriteLine(Mean<decimal>([10m, 20m, 25m]));
Console.WriteLine(Mean<Half>([(Half)1, (Half)2]));
static T Mean<T>(ReadOnlySpan<T> values) where T : INumber<T>
{
T sum = T.Zero; // static abstract property
foreach (T v in values)
sum += v; // static abstract operator +
return sum / T.CreateChecked(values.Length);
}go deeper
Recall that C# operators are static methods and that INumber<T> lets one generic method add, compare and divide any built-in numeric type such as int, double or decimal.
Explain static abstract versus static virtual, the T.Zero call syntax through a constrained type parameter, and why Mean<int> still performs integer division.
Show the boundaries: CS8926 through the interface name, CS8920 as a type argument, the .NET 7 runtime requirement, and when an adder delegate or overloads still fit better.
Treat generic math as an API decision: one generic entry point serves every numeric type a consumer brings, but it requires .NET 7 or later and turns constraints into public contract.
## The gap: static members could not be part of a contract In C#, every operator is a `public static` method, and so are the things a numeric algorithm needs besides operators: a zero to start a sum from, a one, a parser, a conversion from `int`. Before **C# 11**, an interface could make implementers supply **instance** members only. A constraint such as `where T : IComparable<T>` let generic code call `a.CompareTo(b)` on a value, but it could not say "`T` has a `+` operator" or "`T` has a static `Zero`". Code such as `T sum = 0; sum += v;` simply did not compile for a type parameter. Library authors worked around the gap in three ways: - **One overload per type**: `Mean(int[])`, `Mean(long[])`, `Mean(double[])`, `Mean(decimal[])`, with identical bodies, and another copy whenever a new numeric type such as `Half` or `Int128` appeared. - **Passing the operation in**: a `Func<T, T, T>` adder argument, which costs a delegate call per element and an awkward API. - **`dynamic`**: run-time binding with no compile-time check at all. ## What C# 11 added C# 11 allows an interface to declare static members that implementing types must, or may, provide: | Modifier | Meaning | Example in the .NET 7+ library | |---|---|---| | `static abstract` | no body; every implementing type must supply it | `INumberBase<TSelf>.Zero`, `IAdditionOperators<TSelf, TOther, TResult>.operator +` | | `static virtual` | default body in the interface; a type may replace it | `INumber<TSelf>.Clamp`, the `operator checked +` of `IAdditionOperators` | Generic code calls these members **on the type parameter**: `T.Zero`, `T.Parse(text, provider)`, `T.CreateChecked(count)`, and operators simply as `a + b` when `a` and `b` are `T`. The compiler checks the call against the constraint; at run time the implementation of the actual type argument runs, so `Mean<decimal>` uses `decimal`'s addition and division. For value-type arguments the JIT compiles a dedicated copy of the method, so there is no boxing and no interface dispatch per element. The feature needs runtime support as well as compiler support: targeting an older runtime gives error CS8919, "Target runtime doesn't support static abstract members in interfaces." ## How Mean<T> uses them ```csharp static T Mean<T>(ReadOnlySpan<T> values) where T : INumber<T> { T sum = T.Zero; foreach (T v in values) sum += v; return sum / T.CreateChecked(values.Length); } ``` Each line leans on a member that `INumber<T>` inherits: 1. `T.Zero` is a `static abstract` property of `INumberBase<T>`. 2. `sum += v` uses `operator +` from `IAdditionOperators<T, T, T>`. 3. `/` uses `IDivisionOperators<T, T, T>`. 4. `T.CreateChecked(values.Length)` converts the `int` count into `T`, throwing `OverflowException` if it does not fit. The arithmetic is always `T`'s own. `Mean<int>` of 1, 2, 3 and 4 returns **2**, because `int` division truncates; generic math never promotes to `double` behind your back. ## Where such an interface can and cannot appear - Through the interface name: `INumber<int>.Zero` fails with **CS8926**, "A static virtual or abstract interface member can be accessed only on a type parameter." - As a type argument: `new List<INumber<int>>()` fails with **CS8920**, because a generic method could otherwise call a static member on the interface itself, where no implementation exists. - As a variable type: `INumber<int> boxed = 5;` compiles and boxes the `int`, but through that variable you reach only instance members such as `CompareTo`. - On a built-in type: `int` implements many of these members explicitly, so `int.Zero` does not even exist as a public member; the static members are meant to be reached as `T.Zero`. ## Static virtual: a default the type may replace A `static virtual` member gives generic code a working default while letting a type supply something better. `INumber<T>.Clamp(value, min, max)` is one: its default body throws `ArgumentException` when `min` is greater than `max` and otherwise calls `T.Max` and `T.Min`, so a generic `Fit<T>(T v, T lo, T hi) => T.Clamp(v, lo, hi)` works for every number. `int` replaces that default with its own public static `Clamp`, which is also why ordinary code can write `int.Clamp(150, 0, 100)` and get 100 without any generics. The same mechanism lets the operator interfaces offer a checked `+` that falls back to the regular one. ## What an interviewer listens for - The problem stated in one line: operators and factories are static, and interfaces could not require static members. - The two modifiers, `static abstract` and `static virtual`, and the `T.Member` call syntax through a constrained type parameter. - The release pair: the language feature is C# 11, the `INumber<T>` family is .NET 7. - An awareness that `T`'s own semantics apply, as the integer `Mean` shows.
- Why does Mean<int> of 1, 2, 3 and 4 return 2 rather than 2.5?Because `sum / T.CreateChecked(values.Length)` runs `int`'s own division operator, and integer division truncates 10 / 4 to 2. Generic math dispatches to the type argument's operators; it never widens to `double`. For a fractional mean, add a second type parameter `TResult : INumber<TResult>`, convert each value with `TResult.CreateChecked(v)` and divide in `TResult`.
- What is the difference between a static abstract and a static virtual interface member?A `static abstract` member has no body, and every implementing type must supply it, or the type fails with CS0535. A `static virtual` member has a default body in the interface that runs unless the implementing type declares its own. `INumber<T>.Clamp` and the `operator checked +` of `IAdditionOperators<TSelf, TOther, TResult>` are static virtual; `INumberBase<T>.Zero` is static abstract.
- Can a method take a parameter of type INumber<int>?Yes. The interface is a legal parameter or variable type, and passing an `int` boxes it. Through that reference you can call only instance members such as `CompareTo`; the static members stay out of reach, because they are only accessible on a type parameter (CS8926). Using the same interface as a type argument, as in `List<INumber<int>>`, is CS8920.
- A where T : struct constraint already lets generic code use + on T.
- Static abstract members can be called as INumber<int>.Zero through the interface name.
- INumber<T> boxes every value to call its operators.
- Mean<int> returns a fractional result because generic math promotes to double.
- Generic math needs only a newer compiler, so any older runtime can run it.