This project is read-only.
4
Vote

Optional trailing commas in method/constructor calls

description

One very small but appreciated feature in C# is the tolerance of the compiler for trailing commas in array initialization or object initialization:
var a = new int[] { 3, 4, 5, };
var b = new { Bark = "woof", Bite = "chomp", }
This makes it easier to reorder or edit the item order without taking time to make sure that last comma gets deleted.

It would be nice if this feature also extended to method and constructor invocation. It would be especially useful in the post C# 4.0 where named parameters can be reordered or used to make a highly descriptive method call:
void Foo (int bar, String baz, Func<int> cat, Action<String> dot, int elf) { }

Foo(
    bar: 5, 
    baz: "",
    cat: () => 6,
    dot: p => Console.WriteLine(p),
    elf: 10
);

//Wait, having elf last is confusing, let me group it with bar

//Rather than just copy and paste the relevant line...
Foo(
    bar: 5, 
    elf: 10, //Now I need to add this comma here!
    baz: "",
    cat: () => 6,
    dot: p => Console.WriteLine(p) //And I need to remove this comma here!
);
It would be great if method invocations were also tolerant of an extra comma.
Foo(
    bar: 5, 
    elf: 10, 
    baz: "",
    cat: () => 6,
    dot: p => Console.WriteLine(p), //Extra comma here, it's not hurting anyone!
);

comments

SSL wrote May 28, 2014 at 3:50 AM

I don't think this is a good idea.

Trailing commas make sense in array & object initializers because they are by nature variable-sized.

Method calls, by contrast, are far more fixed-length (even overloads & optional parameters are generally limited), so an extra comma is more likely to indicate a mistake.

The current behavior is also consistent with other languages that allow this (like Javascript).

It might make more sense to allow this for params parameters only.