CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Jeremy D. Miller -- The Shade Tree Developer

Under the hood and working with .Net, TDD, Software Design, and Agile Stuff

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.



Comments

jmarlowe said:

Um... it is.

Fired!

# September 24, 2007 7:24 PM

ScottGu said:

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

# September 24, 2007 9:23 PM

Jeremy D. Miller said:

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

# September 24, 2007 9:48 PM

Brett said:

its been in 2.0

# September 24, 2007 10:04 PM

cmyers said:

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

# September 24, 2007 10:32 PM

BCS said:

the use is in code like this

new string[]{

"this",

"is",

"the",

"last",

//"part"

};

the comment at the end doesn't break it.

# September 24, 2007 11:35 PM

cmyers said:

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

# September 25, 2007 1:11 AM

Jeremy D. Miller said:

@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.

# September 25, 2007 8:35 AM

Tom said:

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'

# September 30, 2007 5:18 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Jeremy D. Miller

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 previously worked as a systems architect building mission critical supply chain software for a Fortune 100 company and learned agile development practices as a .Net consultant at ThoughtWorks, one of the pioneers of agile development. Jeremy is the author of the open source StructureMap (http://structuremap.sourceforge.net) tool for Dependency Injection with .Net and the forthcoming StoryTeller (http://storyteller.tigris.org) tool for supercharged FIT testing in .Net. Jeremy's thoughts on just about everything software related can be found on his weblog "The Shade Tree Developer" at http://codebetter.com/blogs/jeremy.miller, part of the popular CodeBetter site. Jeremy is a Microsoft MVP for C#. Check out Devlicio.us!

This Blog

Syndication

News

All opinions expressed here constitute my (Jeremy D. Miller's) personal opinion, and do not necessarily represent the opinion of any other organization or person, including (but not limited to) my fellow employees, my employer, its clients or their agents.

About Me

"Best Of" Compendium

StructureMap (Dependency Injection for .Net)

StoryTeller (Supercharged Fit)

Build your own Cab

TestDriven

MVP