Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does param object[] array become jagged if you pass an int[] array into it?

As I was experimenting with params, I noticed the MS documentation says if you pass an array of int as a method parameter that has a signature of params object[] myParam, it will become a multi-dimensional array. But I noticed if you pass an array of objects[] or strings[] it does not. This seems like it would be a headache to work with, as you have to check for multi-dim arrays.

See MSDN link

Example:

public static void UsingParams(params object[] myParam)
{
    //Code to return myParam
}

//myParam[0][0] = {1, 2}, multi-dimensional
int[] myIntArray = {1, 2};
UsingParams(myIntArray);

//myParam[0] = {"1", "2"}, single-dimensional
string[] myStrArray = {"1", "2"};
UsingParams(myStrArray);

Why does this occur?

like image 565
johnjk07 Avatar asked Nov 06 '14 20:11

johnjk07


People also ask

What is jagged array how it is declared in C#?

A jagged array is an array whose elements are arrays, possibly of different sizes. A jagged array is sometimes called an "array of arrays." The following examples show how to declare, initialize, and access jagged arrays.

Why do we need jagged array?

Jagged arrays are a special type of arrays that can be used to store rows of data of varying lengths to improve performance when working with multi-dimensional arrays. An array may be defined as a sequential collection of elements of the same data type. The elements of an array are stored in contiguous memory locations ...

What is jagged array in C# when to prefer jagged arrays over multi dimensional arrays?

In a multidimensional array, each element in each dimension has the same, fixed size as the other elements in that dimension. In a jagged array, which is an array of arrays, each inner array can be of a different size. By only using the space that's needed for a given array, no space is wasted.

What is jagged arrays explain with suitable example?

Jagged array is a multidimensional array where member arrays are of different size. For example, we can create a 2D array where first array is of 3 elements, and is of 4 elements. Following is the example demonstrating the concept of jagged array.


2 Answers

Whenever you have a params parameter the compiler will attempt to accept an array representing all of the values for the params argument, if the parameter at the position in question is valid in that context. If it's not, then it tries to treat it as one item in the array of params values, rather than as the whole array. If it can't do that either then it will fail to compile.

In your second example a string[] can be implicitly converted to an object[], so it is passed as the entire list of parameters. This implicit conversion is valid because of array covariance.

In your first example the int[] cannot be implicitly converted to an object[] (array covariance is limited to reference types), so it is treated as one value in the array. An int[] can be implicitly converted to object, so what is passed is an object array containing an int[] as its only item. Note that an array with another array as an item is dramatically different from a multi-dimensional array.

like image 156
Servy Avatar answered Oct 10 '22 05:10

Servy


C# is trying to figure out, when you only pass one value to a params argument, whether you mean for that value to be the array represented by the argument, or whether you're passing it the first argument of a larger array.

If you remove the params keyword, you'll see that int[] cannot be converted directly into an object[] (due to int being a non-reference type):

enter image description here

So C# figures it must just be the first of your params that you're passing in, rather than the entire array. It converts your code to this:

int[] myIntArray = {1, 2};
UsingParams(new object[]{myIntArray});
like image 2
StriplingWarrior Avatar answered Oct 10 '22 04:10

StriplingWarrior