Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
This may be an old and well-known thing but I just learned that I can write an extra comma in an object initializer and it's accepted by the compiler.

Consider the following example
C#
namespace ConsoleApplication1 {
   class Program {
      static void Main(string[] args) {

         // Create a new object with an extra comma in the initializer
         CommaComa theThing = new CommaComa() { A = 1, }; // no error
      }
   }

   /// <summary>
   /// Some class
   /// </summary>
   public class CommaComa {
      /// <summary>
      /// A property
      /// </summary>

      public int A { get; set; }
      /// <summary>
      /// Another property
      /// </summary>
      public string B { get; set; }

      /// <summary>
      /// Some method
      /// </summary>
      public void Foo(int a, int b = 5) { }
   }
}

As you can see there's an extra comma after the assignment A = 1 but the code compiles just fine.

Now the question is (basically out of curiosity), in what kind of use-cases it would make sense to add the extra comma with nothing after it?

As far as I can see the same isn't allowed outside initializers. If this would be systematic I'd expect something like the following would also work
C#
theThing.Foo( a: 1, ); // Gives Argument missing error
Posted
Updated 7-Oct-15 17:26pm
v2
Comments
Afzaal Ahmad Zeeshan 7-Oct-15 10:42am    
This was something new to me, and seem irritating to me. I would personally try to stick to "not add a comma after the last initializer". :-)

But the question is a lot interesting and I am looking forward to learn something new here.
Richard Deeming 7-Oct-15 11:00am    
Probably not the reason (you'd have to ask Microsoft for that!), but you can do the same thing in Javascript object literals:
http://ecma262-5.com/ELS5_HTML.htm#Section_11.1.5[^]
(IE8 and earlier don't like it, but all modern browsers accept a trailing comma.)

Maybe it is done this way to allow developpers to reorder their assignations without having to bother to put a comma on each end of line except the last one.

Something like:
C#
KarmaComa theThing = new KarmaComa() {
   A = 1,
   B = "Foo",
};

that could be easily refactored to:
C#
KarmaComa theThing = new KarmaComa() {
   B = "Foo",
   A = 1,
};

by simply drag-and-dropping the line.

But this is just a guess.
 
Share this answer
 
v2
Comments
PIEBALDconsult 7-Oct-15 11:04am    
Or put the commas on their own lines...


And I really don't like tiny little commas at the ends of lines -- they're much more visible at the beginning.
phil.o 7-Oct-15 11:10am    
I do that for SQL queries (putting the commas at the beginning of the line).
I almost never use the 'anonym method initialization' on my objects; I use to create a specific constructor when I need to initialize my objects quickly.
Wendelius 7-Oct-15 23:28pm    
This could be one explanation. But why not systematically allow this, why just with initializers...

I marked a 5 on the answer and will accept it in time but I want to leave the question open for awhile in case some other points are raised.

Thanks :)
It's not just allowed by the compiler, it's in the C# specification[^] (See Section 7.6.10)
Quote:
7.6.10.6 Anonymous object creation expressions
An anonymous-object-creation-expression is used to create an object of an anonymous type.
anonymous-object-creation-expression:
new anonymous-object-initializer
anonymous-object-initializer:
{ member-declarator-listopt }
{ member-declarator-list , }

@phil.o has a good point with the re-ordering of the assignments, but I tend more towards the code-generation use ... i.e. not having to remove the final comma from the list.

It works even if all possible properties have been assigned (as per the spec :-)) so it may also have been included for extensibility
 
Share this answer
 
Comments
Wendelius 7-Oct-15 23:31pm    
Yes, I noticed that it's part of the spec but why only with initializers. I modified a question and added an example method call which would be a bit similar situation. However, in such case the extra comma isn't allowed...

I marked a 5 on the answer and will accept it in time but I want to leave the question open for awhile in case some other points are raised.

Thankls :)
CHill60 8-Oct-15 7:41am    
"I want to leave the question open for awhile in case some other points are raised." - Good idea, we might find out something useful :-)
Question 5'd
It would only make sense, because the property assignments are nothing more than key value pairs, and the basis of any kvp collection (not just C#, but any higher language that uses kvp or maps) is to ignore any entries that are null value.
 
Share this answer
 
Comments
Wendelius 7-Oct-15 23:33pm    
Yes, that makes sense, but the question is, why? In what kinds of situations this is useful?
Well, grammar for C# is written that way.
C# allows a trailing comma at the end of an array-initializer, object-initializer and collection-initializer. This syntax provides flexibility in adding or deleting members from such a list, and simplifies machine generation of such lists.

Please read the page 363 from here[^]

-KR
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900