I am making a Matrix class in C#, and would like for it to be able to include any type that implements the INumber interface. I am unsure of how to be able to implement addition for matrices with different generic types e.g. Matrix<int>
and Matrix<float>
.
public class Matrix<T> where T: INumber<T>
{
private int _width, _height;
private T[,] _vals;
... // implementation of the rest of the class
public static Matrix<T> operator+(Matrix<T> first, Matrix<T> second) {
throw new NotImplementedException();// this stuff is fine
}
}
Currently I have this, but this only allows for you to add 2 matrices of the same generic type. I am aware of IAdditionOperators<TSelf, TOther, TResult>
, but I do not know how to use it in my code. I am also unsure of how the new type should be decided, i.e. if I add Matrix<int>
and Matrix<float>
, it should logically return a Matrix<float>
, but I do not know how this would be decided.