Meh*_*dad 8 c# generics boxing interface constraints
让我说我有
interface IMatrix {
    double this[int r, int c] { get; }
}
struct Matrix2x2 : IMatrix  {
    double a1, a2, b1, b2;
    double this[int r, int c] { get { ... } }
}
struct Matrix3x3 : IMatrix {
    double a1, a2, a3, b1, b2, b3, c1, c2, c3;
    double this[int r, int c] { get { ... } }
}
class Matrix : IMatrix {    // Any size
    double[,] cells;
    double this[int r, int c] { get { ... } }
}
有时候,而不仅仅是说
static class Matrices {
    static IMatrix Multiply(IMatrix a, IMatrix b) { ... }
}
我最终做了
static class Matrices {
    static IMatrix Multiply<T1, T2>(T1 a, T2 b)
        where T1 : IMatrix
        where T2 : IMatrix { ... }
}
或者甚至是
static class Matrices {
    static IMatrix Multiply<T1, T2>([In] ref T1 a, [In] ref T2 b)
        where T1 : IMatrix
        where T2 : IMatrix { ... }
}
避免拳击或复制structs.
它工作正常,但是有什么缺点我不知道(除了可忽略不计的内存使用量增加)?这是一种公认的做法,还是因为我可能不知道的任何原因而气馁?