Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using params keyword with a multidimensional array

Why it is compile time error when we use the params keyword with a multidimensional array?

using System;

namespace Testing_Params_Keyword
{
    class Program
    {
        static void Main(string[] args)
        {
            //Calculate in invoked
            Calculate(25.4, 26.2, 27.8, 28.9);
        }

        //Declearing Calculate method
        public static void Calculate(params float [ , ] Money)//----** Here is error **
        {
            //Divide values of column1 by column2
            float row1 = Money[0, 0] / Money[0, 1];
            float row2 = Money[1, 0] / Money[1, 1];

            Console.WriteLine(row1 + row2);
        }//end of method Calculate
    }
}

Gives me the error

The params parameter must be a single dimensional array

Why it must be single dimensional array?

like image 695
mdadil2019 Avatar asked Jan 31 '26 21:01

mdadil2019


2 Answers

params isn't about passing multi dimensional data, it's about passing a variable number of arguments to a function. Since that list of arguments is inherently one dimensional that's why the type has to be a one dimensional array.

like image 124
Sean Avatar answered Feb 02 '26 11:02

Sean


Because everything the C# compiler does, albeit magical- must have some kind of logic behind it. The params keyword just creates an array whose size is the amount of parameters you have passed.. this is something that the compiler can establish.. It can not, however, infer the amount of dimensions which you intend, or even the amount of elements per dimension. Therefore- what you attempt to do could never compile.

like image 29
Eyal Perry Avatar answered Feb 02 '26 11:02

Eyal Perry