【发布时间】:2022-12-14 17:39:00
【问题描述】:
我正在研究一个矩阵类,我想在其中制作一些线性代数函数。我想在矩阵中有一个通用类型,你可以用它做算术运算,因为我想创建一个代表分数的类而不是使用双精度,但我也想在未来使用双精度。像这样:
class Temp<T>
{
T[,] matrix;
// Example of a math-using function
public T Sum()
{
T sum = matrix[0,0];
for(int i = 0; i < matrix.GetLength(0); i++)
{
for(int j = 0; j < matrix.GetLength(1); j++)
{
sum += matrix[i, j]; // Error here
}
}
return sum;
}
}
我以为我可以使用类似 where T : IMathable 的东西,但我无法弄清楚它应该具有哪些继承性。
【问题讨论】:
-
Generic Math 已经预览了一段时间,我相信它确实进入了已发布的 .NET 7
标签: generics inheritance c#-3.0