Building C # Lists from What's Enumerable

var t = new List<byte?[,]>(); var t2 = new byte?[4, 4][]; var r = new List<byte?[,]>(t); var r2 = new List<byte?[,]>(t2); // error 

I thought that C # lists and arrays are enumerable and that lists can be built from an enumerated object to create a copy of the collection.

What happened to the last line of the above example?

Compilation error: the best overloaded method matching for List.List (IEnumerable) has some invalid arguments.

+4
source share
2 answers

If t2 should be an array of 2D arrays (this indicates the purpose of the lists), then the declaration of t2 is incorrect. If you think after:

 var t = new List<int[,]>(); var t2 = new int[10][,]; for (int i = 0; i < t2.Length; ++i) { t2[i] = new int[4, 4]; } var r = new List<int[,]>(t); var r2 = new List<int[,]>(t2); // no error! 
+3
source

What happened to the last line of the above example?

The line throws an error, because t2 is a new byte?[4, 4] array from 2d, since r2 is an array of List of byte?[,] 2d

 var r2 = new List<byte?[,]>(t2); // error 

so the solution will go through the List of byte?[,] in it, like this

 var r2 = new List<byte?[,]>(new List<byte?[,]>()); 

Also t is the corresponding 2d array in the list that can be passed to r2

 var r2 = new List<byte?[,]>(t); 
0
source

All Articles