2

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?

6
  • 2
    Because "The params parameter must be a single dimensional array". Commented Jun 3, 2016 at 9:15
  • Why it must be single dimensional array? Commented Jun 3, 2016 at 9:15
  • 1
    So this is a new question, can you update your post? Commented Jun 3, 2016 at 9:16
  • 1
    you can instead use a own class with the params in it and use this function with a List<yourclass> Commented Jun 3, 2016 at 9:18
  • 3
    How exactly would you expect the code to determine the valid dimensions of the array? Commented Jun 3, 2016 at 9:19

3 Answers 3

10

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.

Sign up to request clarification or add additional context in comments.

Comments

7

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.

Comments

-2

It is because the params varible is based on command line arguments where the command runs after pressing enter. This method does not allow for multiple dimensions and therefore is reflected in the varible as the compiler kbows this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.