自定义约束的类

huangapple go评论66阅读模式
英文:

Class with custom Constraint

问题

以下是您要翻译的代码部分:

class Matrix<T> where T :  /*已定义为所有加法、乘法和减法操作*/
{
    public List<List<T>> Lines { get; set; }
    public Matrix()
    {
        Lines = new List<List<T>>();
    }
    public T this[int line, int column]
    {
        get => Lines
[column];
set => Lines
[column] = value;
} public List<T> this[int line] { get => Lines
;
} private static T LineMultiplyCol(List<T> l, List<T> c) { T index = default; for (int i = 0; i < l.Count; i++) { index += l[i] * c[i]; //这里是错误的地方。 } return index; } public static Matrix<T> operator *(Matrix<T> m1, Matrix<T> m2) { //我想要完成这个部分。 } }

请注意,我只翻译了您提供的代码部分,而不包括其他内容。

英文:

How can I do this? Limit to use defined operation Data Types(T) or
limit to use an T whit Defined for all addition, multiplication and subtraction operations here is my Matrix class

class Matrix&lt;T&gt; where T :  /*Defined for all addition, multiplication and subtraction operations*/
{
    public List&lt;List&lt;T&gt;&gt; Lines { get; set; }
    public Matrix()
    {
        Lines = new List&lt;List&lt;T&gt;&gt;();
    }
    public T this[int line, int column]
    {
        get =&gt; Lines
[column]; set =&gt; Lines
[column] = value; } public List&lt;T&gt; this[int line] { get =&gt; Lines
; } private static T LineMultiplyCol(List&lt;T&gt; l, List&lt;T&gt; c) { T index = default; for (int i = 0; i &lt; l.Count; i++) { index += l[i] * c[i]; //Here is the error. } return index; } public static Matrix&lt;T&gt; operator *(Matrix&lt;T&gt; m1, Matrix&lt;T&gt; m2) { //i want to complete this. } }

this is my program, i want to expend to Parametric Matrix Operations, and need to do it

自定义约束的类

Parametric Matrix Operations and etc.

答案1

得分: 2

.NET 7/C#11引入了新的通用数学。您要查找的约束条件如下:

或者只需使用单个约束条件:INumber&lt;TSelf&gt;

英文:

Introduced in .NET 7/C#11 is the new generic math. The constraints you are looking for are:

Or just use a single constraint: INumber&lt;TSelf&gt;

huangapple
  • 本文由 发表于 2023年6月5日 23:05:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76407757.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定