Quantcast
Channel: .NET Framework Class Libraries forum
Viewing all articles
Browse latest Browse all 8156

params Keyword for 2-Dimensionals

$
0
0

Can the params keyword be used to create two-dimensional arrays? If yes, how?

Reasons:

I created a matrix theory class "Matrix" which is just a bunch of functions on float[,] (such as adjoint, rref, ref, det, ...), but I would like users to be able to construct "Matrix" instances without having to initialize the values by manually calling indeces or without having to first create a float[,].

What I'm doing now:

    //Constructor

    float[,] _matrix;

    public Matrix(int rows, int columns)
    {
      try { _matrix = new float[rows, columns]; }
      catch { throw new MatrixException("invalid dimensions."); }
    }

    Matrix vec3 = new Matrix(3, 1); vec[0,0] = 1; vec3[1,0] = 2; vec[2,0] = 3;

What I want to do:

    // Constructor

    float[,] _matrix;

    public Matrix(params float[] values)
    {
      _matrix = values;
    }

    Matrix vec3 = new Matrix({1}, {2},{3}); // This causes compiler error!

Why?

Sure I could just have users pass in a  float[,], but then they have a reference to the array back in their calling code (inexperienced coders could corrupt the matrix from that array reference). I could clone the float[,] and then set it, but that is slow, and I'm writing this for a game engine :( .

I'm pretty sure I can't do my request, but all I want to do is make my matrix constructor act like a hard-coded float array construct (new float{{1},{2},{3}} == new Matrix({1},{2},{3})).

Thanks.


Viewing all articles
Browse latest Browse all 8156

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>