Silly thing I want from Ruby in C#

The trailing comma should be legal after the last array element like Ruby:  string[] names = new string[]{ “a”,   };


Trivial, but helpful here and there.

About Jeremy Miller

Jeremy is the Chief Software Architect at Dovetail Software, the coolest ISV in Austin. Jeremy began his IT career writing "Shadow IT" applications to automate his engineering documentation, then wandered into software development because it looked like more fun. Jeremy is the author of the open source StructureMap tool for Dependency Injection with .Net, StoryTeller for supercharged acceptance testing in .Net, and one of the principal developers behind FubuMVC. Jeremy's thoughts on all things software can be found at The Shade Tree Developer at http://codebetter.com/jeremymiller.
This entry was posted in Uncategorized. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • Tom

    I’m pretty sure that C had this feature first. Trailing commas are legal in C. The reason I’ve seen quoted for this is that it made ‘machine generation’ of lists easier.

    Ref: ‘Deep C Secrets’

  • http://codebetter.com/blogs/jeremy.miller Jeremy D. Miller

    @Chad,

    I want it for basically the reason above. I have an array that’s more or less a set of directives. I want to be able to CTRL-D and then change a little bit without worrying about the comma. Plus it simplifies codegen a little bit.

    I do get the reason for writing SQL that way, but I learned it like this:

    SELECT
    FIELD1,
    FIELD2,
    FIELD3
    FROM
    SOME_TABLE
    WHERE
    FIELD1 = ‘something’;

    SQL just doesn’t look right to me any other way.

    And the guy doing .Net 1.1 (me) is probably more the Luddite than you.

  • http://community.hdri.net/blogs/chads_blog cmyers

    Hrm, that seems pretty lame for a language feature.

    It also doesn’t play well with line-by-line merge VCS’ like Subversion.

    A DBA I used to work with suggested this for multi-line comma-delimited list:

    “this”
    , “is”
    , “the”
    , “last”
    , “part”

    That way a change to “part” won’t necessitate changing two lines (or the compiler, for that matter)

    Also works well in SQL scripts for list-of-columns, etc

  • BCS

    the use is in code like this

    new string[]{
    “this”,
    “is”,
    “the”,
    “last”,
    //”part”
    };

    the comment at the end doesn’t break it.

  • http://community.hdri.net/blogs/chads_blog cmyers

    For us luddities, why is this helpful? (honest question, I’m afraid I’ve been missing something cool)

  • Brett

    its been in 2.0

  • http://codebetter.com/blogs/jeremy.miller Jeremy D. Miller

    It would help to get out of doing 1.1 legacy code, but I’m glad it’s there in 3.0.

  • http://weblogs.asp.net/scottgu ScottGu

    Hmm – this compiles clean today (at least in C# 3.0)

    BTW, instead of:

    string[] names = new string[]{ “a”, };

    You can also now just write the code below (no need to specify the type twice):

    string[] names = new []{ “a”, };

    Or:

    var names = new string[]{ “a”, };

    Hope this helps,

    Scott

  • jmarlowe

    Um… it is.

    Fired!

  • Thomas Eyde

    A trailing comma is allowed in enums, so why not here? An inconsistent oversight?