20

Possible Duplicate:
Inline property initialisation and trailing comma

Working on one of my projects (C# 4.0, Visual Studio 2010), I've accidentally discovered that code like

var obj = new { field1 = "Test", field2 = 3, }

is compiled and executed OK without any errors or even warnings and works exactly like

var obj = new { field1 = "Test", field2 = 3 }

Why does compiler tolerate the trailing coma in first example? Is this a bug in compiler or such behavior does have some purpose?

Thanks

Community
  • 1
  • 1
Sergey Kudriavtsev
  • 10,328
  • 4
  • 43
  • 68
  • 2
    question already answered .. twice: http://stackoverflow.com/questions/3675173/why-can-you-have-a-comma-at-the-end-of-a-collection-initializer and http://stackoverflow.com/questions/2147333/net-enumeration-allows-comma-in-the-last-field – nowaq Nov 24 '11 at 07:26
  • @nowaq My bad, should have searched SO for "trailing coma", not for "anonymous object trailing coma". – Sergey Kudriavtsev Nov 24 '11 at 07:30
  • Did you notice that it seems to allow trainling semicolons also? – Gabe Nov 24 '11 at 07:32

3 Answers3

46

To determine whether or not it's a bug in the compiler, you need to look at the C# spec - in this case section 7.6.10.6, which clearly allows it:

anonymous-object-creation-expression:    
    new   anonymous-object-initializer

anonymous-object-initializer:  
    { member-declarator-listopt }  
    { member-declarator-list , }

So no, it's not a compiler bug. The language was deliberately designed to allow it.

Now as for why the language has been designed that way - I believe it's to make it easier to add and remove values when coding. For example:

var obj = new { 
    field1 = "test", 
    field2 = 3,
};

can become

var obj = new { 
    field2 = 3,
};

or

var obj = new { 
    field1 = "test", 
    field2 = 3,
    field3 = 4,
};

solely by adding or removing a line. This makes it simpler to maintain code, and also easier to write code generators.

Note that this is consistent with array initializers, collection initializers and enums:

// These are all valid
string[] array = { "hello", };
List<string> list = new List<string> { "hello", };
enum Foo { Bar, }
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks for pointing on the spec; think I might find some other interesting things there if read carefully :) – Sergey Kudriavtsev Nov 24 '11 at 07:26
  • 2
    I think the strongest point here is for code generators. And let's all face it, it's a really nice feature that we wish all languages had. – Scott Rippey Nov 24 '11 at 07:28
  • That's what framework like drupal, etc. does and people label that as bad practice. ??? – codingbiz Feb 20 '13 at 13:47
  • @codingbiz: Who labels *exactly what* as bad practice? – Jon Skeet Feb 20 '13 at 13:56
  • @JonSkeet In drupal, PHP arrays are declared with trailing commas and I have seen it been condemned on this forum. Perhaps by those who don't love drupal. As long as it makes coding easier, I think it's OK. Thanks for replying! – codingbiz Feb 20 '13 at 15:18
  • 1
    @codingbiz: It's entirely possible that the same people would dislike it being used in C#. You shouldn't assume it's *just* a platform bias. – Jon Skeet Feb 20 '13 at 15:21
  • This comes as a real shock to me; I always assumed it was invalid, so I never did it. Thanks for setting it out so clearly. – Jordan Gray Oct 10 '14 at 12:25
  • Is it consistent with non-anonymous object initializers, like `new ClassName { Prop1 = 1, Prop2 = 2, }`? – Triynko Jun 14 '17 at 19:35
  • @Triynko: Yes, it is. – Jon Skeet Jun 14 '17 at 21:55
18

One reason the trailing commas is good is because of Source Compares. If you update the source and use a source compare tool, then the source compare tool will only show 1 line changed (the new field3. If there was no trailing comma, then source compare will show 2 lines changed because you had to add the comma after the number 3.

var obj = new {
  field1 = "Test",
  field2 = 3,
  }

var obj = new {
  field1 = "Test",
  field2 = 3,
  field3 = "New",
  }
Simon
  • 1,211
  • 9
  • 5
4

To make deleting the last field easier, I suppose. There is really no ambiguity introduced in the syntax by doing this, so it just makes life a bit easier.

Jonas Høgh
  • 10,358
  • 1
  • 26
  • 46