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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With