Copying empty arrays: the difference between Mono and .Net Core

I have some .Net code whose tests passed through Mono, but as soon as I ran it on .Net Core, several tests failed. I ended up tracing the failed tests for their root cause and found something rather interesting that I would like to ask about here where more experienced .Net encoders can help me.

In fact, the main reason for the failure was that I copied an empty array of type A to the target array of type B. (I will refrain from explaining why I copied an empty array, t should get through the page after the code page. This simplifies the code, most of the time the source array is of type B, but sometimes the creation code creates an empty array of type A). Mono said, “Oh, the array is empty, so I won’t throw a type error,” and the copy was successful. But when I ran tests on .Net Core, I threw a copy of an empty array of type A into the destination array of type B System.ArrayTypeMismatchExceptionwith the message:

The type of the source array cannot be assigned to the type of the target array.

Here is the code so I can show you what I mean in the code, not in English. (In F # code, in particular, from the language I worked with when I came across this interesting difference between Mono and .Net Core).

let someStrs : string[] = [|"a"; "b"; "c"|]
let someInts : int[] = Array.zeroCreate 3
someStrs.CopyTo(someInts, 0)

For those who don't read F # very well, the first line creates an array of three lines, as you probably could figure out. (F # uses [|and |]to declare arrays, because the brackets [and ]are important for determining the linked list.) The second line creates an array of three ints, initialized by default costs. For ints, this is 0, so this creates an array [|0; 0; 0|], but if I were to say let someStrs : string[] = Array.zeroCreate 3, the resulting array would be [|null; null; null|].

, , : . , Mono, .Net Core. :

let someStrs : string[] = [||]
let someInts : int[] = Array.zeroCreate 3
someStrs.CopyTo(someInts, 0)

, , someStrs , 0. - string[], int[] ? , , .Net Core, Mono.

. Windows , , .Net Framework (, 4.7.1, Mono 5.10 .Net 4.7.1 . net461, , ,.NET Framework 4.6. 1 - ).

, : ?. Mono, , Mono? .Net Core , , 0, "", ? .Net/#/F #, , , .Net CLR. . >

+4

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


All Articles