Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a reason for array declaration syntax in C#?

I was wondering what's the reason behind the syntax? I mean it's like this:

int[ , ] arrayName = new int[10,10];

Wouldn't it be simpler if we had something like:

int[10,10] arrayName ;

What caused the software architect to make such a decision?

like image 220
Sam Avatar asked Aug 02 '26 01:08

Sam


1 Answers

It's because you can declare the variable in one scope, and initialize it in another (normally in a constructor).

Something like this:

public class Foo
{
    private int[ , ] Bar;  // Bar will be null until initialized

    public Foo(int a, int b)
    {
        Bar = new int[a, b];
    }
}
like image 95
Andre Andersen Avatar answered Aug 04 '26 16:08

Andre Andersen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!