Reputation: 3317
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 C# lists and arrays are both Enumerable, and that lists can be constructed from an enumerable object to create a copy of the collection.
Whats wrong with the last line from the example above?
Compile error: The best overloaded method match for 'List.List(IEnumerable)' has some invalid arguments.
Upvotes: 4
Views: 203
Reputation: 11201
Whats wrong with the last line from the example above?
The line throws error because t2 is new byte?[4, 4]
a array of 2d array, well as r2 is a List of byte?[,]
2d array
var r2 = new List<byte?[,]>(t2); // error
so solution will be do pass a list of byte?[,]
in it like this
var r2 = new List<byte?[,]>(new List<byte?[,]>());
Also t is the matching 2d array in a list that can be passed in r2
var r2 = new List<byte?[,]>(t);
Upvotes: 0
Reputation: 6329
If t2
should be an array of 2D arrays (list assignment suggests so) then the declaration of t2
is wrong. If think you are 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!
Upvotes: 3