0

I remember reading about generic type constraints in C# and I would swear there was a way to constrain some generic type T using operator requirements, for example:

public class Matrix<T> where T has +, - defined for T and *, / defined for double
{
    public Matrix(int rows, int columns)
        => (Rows, Columns) = (rows, columns);

    public int Rows { get; set; }
    public int Columns { get; set; }
    ...

    public static Matrix<T> operator +(Matrix<T> m, Matrix<T> n)
    {
        if (m.Rows == n.Rows && m.Columns == n.Columns)
        {
            var matrixSum = new Matrix<T>(m.Rows, m.Columns);
            // Write logic for matrixSum[i, j] = m[i, j] + n[i, j]
            return matrixSum;
        }
        return null;
    }

    public static Matrix<T> operator *(double s, Matrix<T> m)
    {
        if (m is not null)
        {
            var scaledMatrix = new Matrix<T>(m.Rows, m.Columns);
            // Write logic for scaledMatrix[i, j] = s * m[i, j]
            return scaledMatrix;
        }
        return null;
    }

    public static Matrix<T> operator *(Matrix<T> m, double s) => s * m;

    public static Matrix<T> operator /(Matrix<T> m, double s) => (1 / s) * m;
    ...
}

I'd like to keep the implementation as generally applicable as possible, so that T could be a numeric type but also any custom type to do linear algebra with, for example complex numbers for which summation and scaling is defined.

Is it possible to have operator constraints like this on a generic type?

JansthcirlU
  • 688
  • 5
  • 21
  • 2
    No, there's no way to constrain a generic like that. Operators are static so you can't even put them in a custom interface. You could make an interface that has methods called `Add`, `Multiply` etc. – DavidG Jun 03 '21 at 12:53
  • Actually you can put operators in interfaces now, with the default implementation, and you can even add a constraint on a type or method to take that interface, and then use those operators ... with some casting, but it still won't allow you to declare `Matrix` which I suspect is the goal of the code in the question, simply because `double` does not implement that interface. – Lasse V. Karlsen Jun 03 '21 at 13:01

0 Answers0