Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# 3 dimensional array definition issue

Tags:

arrays

c#

My following code has compile error,

Error 1 Cannot implicitly convert type 'TestArray1.Foo[,,*]' to 'TestArray1.Foo[][][]' C:\Users\lma\Documents\Visual Studio 2008\Projects\TestArray1\TestArray1\Program.cs 17 30 TestArray1

Does anyone have any ideas? Here is my whole code, I am using VSTS 2008 + Vista 64-bit.

namespace TestArray1
{
    class Foo
    {    
    }

    class Program
    {
        static void Main(string[] args)
        {
            Foo[][][] foos = new Foo[1, 1, 1];

            return;
        }
    }
}

EDIT: version 2. I have another version of code, but still has compile error. Any ideas?

Error   1   Invalid rank specifier: expected ',' or ']' C:\Users\lma\Documents\Visual Studio 2008\Projects\TestArray1\TestArray1\Program.cs 17  41  TestArray1
Error   2   Invalid rank specifier: expected ',' or ']' C:\Users\lma\Documents\Visual Studio 2008\Projects\TestArray1\TestArray1\Program.cs 17  44  TestArray1


namespace TestArray1
{
    class Foo
    {    
    }

    class Program
    {
        static void Main(string[] args)
        {
            Foo[][][] foos = new Foo[1][1][1];

            return;
        }
    }
}

EDIT: version 3. I think I want to have a jagged array. And after learning from the fellow guys. Here is my code fix, and it compile fine in VSTS 2008. What I want is a jagged array, and currently I need to have only one element. Could anyone review whether my code is correct to implement my goal please?

namespace TestArray1
{
    class Foo
    {    
    }

    class Program
    {
        static void Main(string[] args)
        {
            Foo[][][] foos = new Foo[1][][];
            foos[0] = new Foo[1][];
            foos[0][0] = new Foo[1];
            foos[0][0][0] = new Foo();
            return;
        }
    }
}

thanks in advance, George

like image 733
George2 Avatar asked Dec 04 '22 15:12

George2


2 Answers

Regarding version 2 of your code - unfortunately you can't specify jagged arrays in that way. Instead, you need to do:

Foo[][][] foos = new Foo[1][][];
foos[0] = new Foo[1][];
foos[0][0] = new Foo[1];

You have to populate each array separately, basically. Foo[][][] means "an array of arrays of arrays of Foo." An initialization statement like this is only capable of initializing one array at a time. With the rectangular array, you still end up with just a single (multi-dimensional) array, which is why new Foo[1,1,1] is valid.

If this is for real code by the way, I'd urge you to at least consider other design decisions. Arrays of arrays can be useful, but you can easily run into problems like this. Arrays of arrays of arrays are even nastier. There may be more readable ways of expressing what you're interested in.

like image 137
Jon Skeet Avatar answered Dec 20 '22 14:12

Jon Skeet


Make up your mind :)

You either want:

Foo[,,] foos = new Foo[1, 1, 1];

or:

Foo[][][] foos = new Foo[1][1][1];
like image 29
Jon Grant Avatar answered Dec 20 '22 16:12

Jon Grant