C# › Interfaces & Abstract Classes
How do static abstract interface members (C# 11+) enable generic math and other 'static polymorphism' patterns?
AdvancedAnswer
Interfaces can now declare 'static abstract' members (like operators or factory methods) that IMPLEMENTING TYPES must provide as their own static members — this lets generic code constrained to such an interface call STATIC operations (like T.Zero, or the + operator) on the generic type parameter itself, something previously impossible since static members couldn't be part of a traditional interface contract.
public interface IShapeFactory<TSelf> where TSelf : IShapeFactory<TSelf> {
static abstract TSelf CreateDefault();
}
public class Circle : IShapeFactory<Circle> {
public static Circle CreateDefault() => new Circle { Radius = 1.0 };
public double Radius { get; set; }
}
T CreateShape<T>() where T : IShapeFactory<T> => T.CreateDefault(); // calls the STATIC method genericallyReal-world example Enabling .NET's own generic math interfaces (INumber<T>, IAdditionOperators<T,T,T>) which rely entirely on static abstract members.