Why does the param object [] array become uneven if you pass the int [] array to it?

Since I experimented with params , I noticed that the MS documentation says that if you pass an int array as a parameter to a method that has the signature params object[] myParam , it will become a multidimensional array. But I noticed that if you pass an array from objects[] or strings[] , it is not. It seems that this will be a headache that you need to work with, since you need to check out the multi-display 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 is this happening?

+6
source share
3 answers

Whenever you have a params parameter, the compiler will try to accept an array representing all the values โ€‹โ€‹for the params argument if the parameter at the appropriate position is valid in this context. If this is not the case, then he tries to consider it as one element in the array of params values, and not as the whole array. If he cannot do this, then he cannot compile.

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

In the first example, int[] cannot be implicitly converted to object[] (the covariance of the array is limited to reference types), so it is considered as a single value in the array. int[] can be implicitly converted to object , so an array of objects containing int[] as the only element is passed. Note that an array with another array as an element is very different from a multidimensional array.

+14
source

C # tries to figure out when you pass only one value to the params argument, do you think that this value is the array represented by the argument, or if you pass its first argument to a larger array.

If you delete the params , you will see that int[] cannot be converted directly to object[] (due to int being a non-reference type):

enter image description here

Thus, C # digits should be the first of your parameters that you pass, not the entire array. It will convert your code to this:

 int[] myIntArray = {1, 2}; UsingParams(new object[]{myIntArray}); 
+2
source

Basically, your method signature takes one or more objects and combines them into an array called myParam .

If several objects are transmitted individually, for example, UsingParams(1, "hello", ...) , they are automatically converted to the object[] array. This is a trick / syntax sugar compiler.

If the transferred object is not an array of object[] or a list of individual objects, it will become the first argument of your array. In other words, if you pass int[] , then your myParam will be an array whose first element is an array, which makes it a jagged array. This is because int[] is an object , and the compiler is not smart enough to understand what you are doing, and makes it the only element of the object[] array. There is no implicit cast from int[] to object[] , and therefore this does not happen.

The only time you can pass an array that will fill up as you expected is when the array type is object[] , for example new object[] { 1, "hello", ... } or the array type is covariant. In this case, the string[] array is covariant and can be implicitly transferred to object[] , but int[] cannot.

In short:

UsingParams(1, "hello") = good

UsingParams(new object[] { 1, "hello" }) = good

UsingParams(new string[] { "hi", "hello" }) = good (due to covariance of the array)

UsingParams(new int[] { 1, 2 }) = bad (no array covariance), will be a jagged array

Further reading of the rules for array covariance , which also says: โ€œThe covariance of arrays does not specifically apply to arrays of type values. For example, there is no conversion, which allows int[] be processed as object[] .

+1
source

Source: https://habr.com/ru/post/977850/


All Articles