Why doesn't the compiler complain about extra "," in Array or Object Initializers?

Using a simple type type

class A {
  public int X, Y;
}

with lens intializers, you can write

var a = new A { X=0, Y=0 };

But the compiler also accepts the following:

var a = new A { X=0, Y=0, }; // notice the additional ','

Same for int[] v = new int[] { 1, 2, };

It looks a little strange ... Have they forgot to abandon the extra "," in the compiler or is there a deeper meaning?

+5
source share
2 answers

There is nothing deep, it is a common thing accepted by compilers of many (but not all) languages. This facilitates the execution of lists:

var a = new A {
    X = 0,
    Y = 0,
};

Z = 0, , . deltas , .

+5

. .

+1

All Articles