1

Sounds simple and it probably is.

I have this variable:

byte[,,] data = new byte[360,288]

And I want 4 for them.

I do not want this though:

byte[,,,] data = new byte[360,288,4]

I prefer this:

byte[,,][] data = new byte[360,288][4]

Is it possible?

5
  • msdn.microsoft.com/en-gb/library/2yd9wwz4.aspx Commented Aug 2, 2014 at 18:40
  • 2
    This is a special case of jagged arrays. Commented Aug 2, 2014 at 18:42
  • @Damian Thanks for the link but it does not solve my question unfortunately. Commented Aug 2, 2014 at 18:43
  • @BlueTrin at least I know what to Google now! Thanks Commented Aug 2, 2014 at 18:43
  • Look at my answer I think it is what you need, I inverted the dimensions but you could do it with a nested for if you want to keep the dimensions you have shown in your example. Commented Aug 2, 2014 at 18:47

1 Answer 1

2

Yes this is a special case of jagged arrays, one where one of the jagged dimension is multidimensional.

You should write something like this:

        // Initialise 4 arrays of two dimensional arrays
        byte[][,] data = new byte[4][,];
        // Initialise the arrays
        for (var i = data.GetLowerBound(0); i <= data.GetLowerBound(0); ++i)
            data[i] = new byte[360, 258];

Of course you can invert the dimensions if you need it.

        // Initialise 4 arrays of two dimensional arrays
        byte[,][] data2 = new byte[360,258][];
        // Initialise the arrays
        for (var i = data2.GetLowerBound(0); i <= data2.GetLowerBound(0); ++i)
            for (var j = data2.GetLowerBound(1); j <= data2.GetLowerBound(1); ++j)
                data2[i,j] = new byte[4];
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, yes I did a Google search and implemented it. It matches your answer so thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.