"converting multidimensional array elements to different type" Code Answer

5

you have an array, which contains two arrays, each of which contains a different number of float arrays.

array.convertall is suitable for converting one array into the other by specifying a mapping delegate (one to one). in this case, you don't only have to convert a single float[,] into a vector2. note that you also used a float[] to vector2 converter, instead of float[,] to vector2.

multidimensional arrays like float[,] are also a bit tricky since they don't support linq out of the box, which it a bit harder to create a one-liner which would do all the mapping.

in other words, you will at least need a helper method to map the items of the multidimensional array:

public static ienumerable<vector2> convertvectors(float[,] list)
{
    for (int row = 0; row < list.getlength(0); row++)
    {
        yield return new vector2(list[row, 0], list[row, 1]);
    }
}

and then you can use that inside the array.convertall method:

var converted = array.convertall<float[,], vector2[]>(
    vertices,
    ff => convertvectors(ff).toarray());

honestly, i would prefer a linq solution because it will infer all the generic parameters automatically:

var r = vertices
    .select(v => convertvectors(v).toarray())
    .toarray();
By ibzib on March 3 2022

Answers related to “converting multidimensional array elements to different type”

Only authorized users can answer the Search term. Please sign in first, or register a free account.