Closed vladmeici closed 7 months ago
Most languages, including C#, allow the code style of trailing commas. It is not wrong.
When multiple items are separated by the comma, for example, when declaring an anonymous object, an array, collection initializers, enums, and switch expressions, C# allows you to have the trailing comma after the last item. This makes it easy to rearrange the order without having to keep adding and removing commas.
Here is the discussion about allowing trailing commas for switch expressions: https://github.com/dotnet/csharplang/issues/2098
Even JSON serializers have an option to allow this because it is so common to use. https://learn.microsoft.com/en-us/dotnet/api/system.text.json.jsonserializeroptions.allowtrailingcommas
I do point this out on page 446 but in the next edition I will move that note earlier in the book to when I first use the technique.
I've added an improvement item for this: https://github.com/markjprice/cs12dotnet8/blob/main/docs/errata/improvements.md#page-144---list-pattern-matching-with-arrays
Thanks Mark for the information.
Hi Mark,
On the page 144, there is a comma for the last switch statement, at the end of the page:
[...] => "Any items in any order.",
Even if the code is working like this, the comma gives you the feeling that something more will come.Best, Vlad