在我看来,这不仅是我的问题。
请不要关闭我的问题作为重复,因为我看了这些问题,我没有找到解决方案。
如何为泛型类重载运算符?
class Matrix<T> {
private Int32 rows;
private Int32 cols;
private T[,] matrix;
public Matrix() {
rows = cols = 0;
matrix = null;
}
public Matrix(Int32 m, Int32 n) {
rows = m;
cols = n;
matrix = new T[m, n];
}
public T this[Int32 i, Int32 j] {
get {
if (i < rows && j < cols)
return matrix[i, j];
else
throw new ArgumentOutOfRangeException();
}
protected set {
if (i < rows && j < cols)
matrix[i, j] = value;
else
throw new ArgumentOutOfRangeException();
}
}
public Int32 Rows {
get { return rows; }
}
public Int32 Cols {
get { return cols; }
}
public static Matrix<T> operator+(Matrix<T> a, Matrix<T> b) {
if(a.cols == b.cols && a.rows == b.rows) {
Matrix<T> result = new Matrix<T>(a.rows, a.cols);
for (Int32 i = 0; i < result.rows; ++i)
for (Int32 j = 0; j < result.cols; ++j)
result[i, j] = a[i, j] + b[i, j];
return result;
}
else
throw new ArgumentException("Matrixes don`t match operator+ requirements!");
}
public static Matrix<T> operator-(Matrix<T> a, Matrix<T> b) {
if (a.cols == b.cols && a.rows == b.rows) {
Matrix<T> result = new Matrix<T>(a.rows, a.cols);
for (Int32 i = 0; i < result.rows; ++i)
for (Int32 j = 0; j < result.cols; ++j)
result[i, j] = a[i, j] - b[i, j];
return result;
}
else
throw new ArgumentException("Matrixes don`t match operator- requirements!");
}
你知道什么是编译器会告诉:“操作‘ - ’不能应用于类型‘T’和‘T’的操作数”,这是所有运营商。
那么,对此最好的决定是什么?据我所知,接口不能包含运算符,所以唯一的方法就是抽象基类为T类型,但也正如我所发现的那样,运算符不能被定义为抽象。
@ShuggyCoUk - 实际上,`动态`(在C#4.0)*不支持*运营商。所以这*会起作用,但会变慢。 – 2009-04-24 11:51:41
对不起 - 我应该更清楚一点。动态仍然不会让你在泛型类型上做它,它可以让你*动态*动态_然后_做任何支持它的操作。如果最终用户只想使用正确的操作符(删除所有编译检查),那么这很好。如果他们想要获得远远接近基元或结构的性能(可能的情况),他们不会。 – ShuggyCoUk 2009-04-24 13:41:11