Why has to be size of dynamically-allocated array a static field?

Posted by Ondrej Slinták on Stack Overflow See other posts from Stack Overflow or by Ondrej Slinták
Published on 2010-04-03T12:23:51Z Indexed on 2010/04/03 12:33 UTC
Read the original article Hit count: 208

Filed under:
|
|

I have a dummy class where I am testing arrays. I've noticed that when I want to dynamically allocate size of array at runtime, fields that indicate this size have to be static. I know I should probably use collections for this kind of code, but I'm more interested why do these fields have to be static? Is there any particular reason behind this?

class Foo
{
    private static int x;
    private static int y;

    private int[,] bar = new int[ x, y ];

    public Foo( int a, int b )
    {
        x = a;
        y = b;
    }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET