Using simple type like
class A {
public int X, Y;
}
with object intializers, one can write
var a = new A { X=0, Y=0 };
But the following is also accepted by the compiler:
var a = new A { X=0, Y=0, }; // notice the additional ','
Same for int[] v = new int[] { 1, 2, };
This looks a bit strange ... Did they forgot to reject the additional ',' in the compiler or is there a deeper meaning behind this?
There isn't anything deep, it's a common thing accepted by compilers of many (but not all) languages. This makes doing lists easier:
var a = new A {
X = 0,
Y = 0,
};
When you want to add Z = 0
, you don't need to edit the previous line to add a comma. This improves source code control deltas, because there is only one new line instead of one new line and one changed line.
This also simplifies an implementation of code generators. They don't have to check the last comma.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With