Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
一般二维矩阵长度(0)C#_C#_Arrays_Generics_Matrix - Fatal编程技术网

一般二维矩阵长度(0)C#

一般二维矩阵长度(0)C#,c#,arrays,generics,matrix,C#,Arrays,Generics,Matrix,我试图构造一个二维矩阵,其中T是数字(int,float decimal),但当我试图从二维数组T[,]创建构造函数时,它不允许我获取长度(0)和长度(1),仅获取长度。然后,我需要对矩阵的实例进行减法、乘法和加法,但如果没有这两个维度,我就无法遍历它们。 编译器错误:应为方法、委托或事件。谢谢 using System; public class Matrix<T> where T : struct, IComparable<T>, ICo

我试图构造一个二维矩阵,其中T是数字(int,float decimal),但当我试图从二维数组T[,]创建构造函数时,它不允许我获取长度(0)和长度(1),仅获取长度。然后,我需要对矩阵的实例进行减法、乘法和加法,但如果没有这两个维度,我就无法遍历它们。 编译器错误:应为方法、委托或事件。谢谢

using System;
public class Matrix<T>
    where T : struct,
     IComparable<T>, 
     IConvertible, 
     IEquatable<T>, 
     IFormattable
{
    readonly T[,] matr;
    public int rows;

    public int Rows
    {
        get { return rows; }
    }

    public int Cols
    {
        get { return cols; }
    }

    public int cols;

    public Matrix(T[,] table)
    {
        matr = table;
        rows = matr.Length(0);//problem here
        cols = matr.Length(1);//problem here
    }
使用系统;
公共类矩阵
其中T:struct,
我可比,
i可转换,
合理的,
可攻击的
{
只读T[,]matr;
公共int行;
公共整数行
{
获取{返回行;}
}
公共int Cols
{
获取{return cols;}
}
公共int cols;
公共矩阵(T[,]表)
{
matr=表格;
rows=matr.Length(0);//这里有问题
cols=matr.Length(1);//这里的问题
}

参考

不是直接的答案,但还是要考虑一下:不幸的是,C#不支持“number”类型的模板参数。以下内容不会在矩阵类中编译:

 public static Matrix<T> Add(Matrix<T> a, Matrix<T> b)
 {
     Matrix<T> ret;
     for(...)
     {
         ret[i, j] = a[i,j] + b[i, j]; // Error: No operator + defined for type T
     }
 }
公共静态矩阵添加(矩阵a、矩阵b)
{
矩阵ret;
对于(…)
{
ret[i,j]=a[i,j]+b[i,j];//错误:没有为类型T定义运算符+
}
}
没有可以添加到
where
子句中的接口来告诉编译器T必须支持运算符+

据我所知,只有两种方法可以解决这个问题:为double、float、integer编写三个完全不同的类,或者使用
dynamic
类型。后者得到最好的代码,但可能会对数学库的性能造成严重影响。

公共类矩阵
public class Matrix<T>
    where T : struct, 
        IComparable<T>,
        IConvertible,
        IEquatable<T>,
        IFormattable
{
    private readonly T[,] matr;
    public int rows;

    public int Rows
    {
        get { return rows; }
    }

    public int Cols
    {
        get { return cols; }
    }

    public int cols;

    public Matrix(T[,] table)
    {
        matr = table;
        rows = matr.GetLength(0);
        cols = matr.GetLength(1);
    }
其中T:struct, 我可比, i可转换, 合理的, 可攻击的 { 私有只读T[,]matr; 公共int行; 公共整数行 { 获取{返回行;} } 公共int Cols { 获取{return cols;} } 公共int cols; 公共矩阵(T[,]表) { matr=表格; 行=matr.GetLength(0); cols=材料长度(1); }
添加一些解释而不仅仅是代码摘录会更有帮助。迟到10分钟有点太晚了,除非你给答案带来更多答案。米奇·麦特完美地回答了这个问题。谢谢你提供的信息。将使用dynamic。
public class Matrix<T>
    where T : struct, 
        IComparable<T>,
        IConvertible,
        IEquatable<T>,
        IFormattable
{
    private readonly T[,] matr;
    public int rows;

    public int Rows
    {
        get { return rows; }
    }

    public int Cols
    {
        get { return cols; }
    }

    public int cols;

    public Matrix(T[,] table)
    {
        matr = table;
        rows = matr.GetLength(0);
        cols = matr.GetLength(1);
    }