syntax - Why does C# allow trailing comma in collection initializers but not in params? -
syntax - Why does C# allow trailing comma in collection initializers but not in params? -
valid syntax:
var test = new list<string> { "a", "b", "c",//valid trailing comma };
invalid syntax:
private void test(params string[] args) { } test( "a", "b", "c",//invalid trailing comma );
is matter of syntax inconsistency or calculated decision?
so i'll take stab @ though never know "true" reason wasn't on compiler team - , likelihood of 1 turning questionable.
trailing commas useful in few scenarios, namely merging , code-generation. in context of stuff collection or property initialisers , enums, leaving trailing comma harmless (the compiler can safely infer "end of list" there closing block bracket can hook on to.
method parameters quite explicit - compiler needs lot of command in area can provide feedback when people coding , other ancillary features. leaving trailing comma on method arguments doesn't add together of value above , start cause confusion on how handle "incomplete" code (did user leave there intentionally or type in next argument?).
you right params
fall conceptual gap in see them array, , can specify them comma delimited (which supported prior collection initialisers). why depart in style collection initialisers?
the language spec params
bit doesn't explicitly specify trailing comma support, though collection initialisers parity towards other languages (c++ think), increases familiarity developers migrating c# elsewhere.
my supposition: fact wasn't in spec causes yagni apply, , point forwards value proposition feature no-brainer in favour of not implementing it.
c# syntax comma
Comments
Post a Comment