STT - Extra comma in collection initialisers and enums

   edit
Follow


You all know what STL is (ok, not all of you, some have skipped C++ altogether and some have only created a few Carnivore/Herbivore classes for Uni).

But you don’t know what STT is.

Well I’ll tell you, just sit nicely and listen.

STT is “Silly Tip & Trick”.

This is one:

The Extra comma STT You probably already know of the collection initialiser syntax introduced into C# 3.0:

stt_comma 1

At times, you’d have a long list of items in the initialiser, and you find yourself doing copy&pastes to add lines, or removing lines from the list. There’s this little extra step you need to do, that is making sure that you have a comma between any two adjacent lines.

That makes the copy&paste a bit annoying.

stt_comma 2

However, it appear that csc.exe would accept an extra trailing comma at the end of the list, so this code is 100% valid:

stt_comma 3

Now it’s easier to text-manipulate the list.

It also works for Enum declarations:

stt_comma 4

So, even though having three cats in the house is busy enough, as far as c# is concerned I can easily add more of these. Not sure what the existing cats would think of that.

Just to make sure it actually works:

stt_comma 5

curiosities:


     Tweet Follow @kenegozi