Phase 8 of 30 · Topic 8.4

C# 11/12/13 Static Abstract Members in Interfaces (Generic Math)

1Concept

`static abstract` members in interfaces enable static polymorphism (Generic Math). Generics can now perform arithmetic (`+`, `-`, `*`) and parse data statically without runtime type checks.

2Architecture Diagram

public interface IAddable<T> where T : IAddable<T>
{
    static abstract T operator +(T a, T b);
}
       │
       ▼
public static T ComputeSum<T>(T[] items) where T : INumber<T> ──> Runs on int, double, decimal!

3Code Example

C# 13 & .NET 9
using System;
using System.Numerics;

public class GenericMathDemo
{
    // Generic math function that works on any number type (int, float, decimal, BigInteger)
    public static T SumCollection<T>(ReadOnlySpan<T> numbers) where T : INumber<T>
    {
        T total = T.Zero;
        foreach (T n in numbers)
        {
            total += n;
        }
        return total;
    }

    public static void Main()
    {
        int[] ints = [10, 20, 30];
        double[] doubles = [1.5, 2.5, 3.5];

        Console.WriteLine($"Integer Sum: {SumCollection<int>(ints)}");
        Console.WriteLine($"Double Sum:  {SumCollection<double>(doubles)}");
    }
}

4Expected Output

Integer Sum: 60
Double Sum:  7.5

5Key Takeaways

  • Static abstract interface members power .NET 7/8/9 `System.Numerics.INumber<T>`.
  • Enables static factory methods on interfaces (`static abstract T Create()`).
  • Compiles to direct zero-overhead CPU instructions.