PrinceOfBorgo
PrinceOfBorgo

Reputation: 590

nested array initializer is expected c#

I have to build a jagged array of 2D arrays but I get the error "A nested array initializer is expected". My code is similar to this:

double[,] a1 = new double[,] { { 1 } };
double[,] a2 = new double[,] { { 2 } };
double[,] a3 = new double[,] { { 3 } };
double[,][] b = new double[,][] { a1, a2, a3 };

Why do I get that error? How can I solve the problem?

Upvotes: 0

Views: 949

Answers (1)

Servy
Servy

Reputation: 203834

A single dimensional array of two dimensional arrays of doubles is a double[][,], not a double[,][]. With your current type it's expecting a two dimensional array of single dimensional arrays of doubles, which isn't what you're providing.

This is exactly why you shouldn't use a type like this. You probably want to have a custom type that composes a two dimensional array, and have a single array of that custom type. It'll be far easier to work with that without confusing yourself.

Upvotes: 2

Related Questions