A syntax feature in enum I didn't know

by Miha Markič 8. February 2007 12:15

Consider this enum declaration.

enum Gender { Female, Male, Alien, }

Do you think it compiles? Note that there is a comma after the last element. I would say that it won't compile, at least it doesn't look OK to me.

Guess what, it compiles and that's so good news. Why is it good news? Simple, if you happen to use code generators (hint: [CodeSmith]) you would know that the last comma is always a pain to remove when you are autogenerating an enum, for example. Even if you know the number of elements you want to include in the enum there is some code required to remove the last coma. Let's take a look at an example from my recent template:

public enum SomeList { <% string[] enumItems = new string[]{"One", "Two", "Three"}; for (int i=0; i<enumItems.Length; i++) { %> <%= enumItems[i ] %><% if (i<enumItems.Length-1) { %>,<% } %> <% } %> }

This code produces this output:

public enum SomeList { One, Two, Three }

To remove the last comma I had to use for instead of much more handy foreach. Furthermore I had to add an entire if clause next to the element (<% if (i<enumItems.Length-1) { %>,<% } %>) which further unnecessarily clutters the code. Quite much noise for a simple loop. Now, consider revisited version that leaves comma:

public enum SomeList { <% string[] enumItems = new string[]{"One", "Two", "Three"}; foreach (string item in enumItems) { %> <%= item %>, <% } %> }

Isn't this much more clear and compact code? The only difference is that output leaves last comma - however as I stated above, this isn't a problem at all.

Tags:

.net

Comments (2) -

Renaud Bompuis
Renaud Bompuis
2/17/2007 8:38:34 AM #

What's wrong with string.Join(",\n", enumItems)?

Reply

Miha Markic
Miha Markic
2/18/2007 12:52:07 AM #

Hi Renaud,

Well, you have to create an array beforhand and store each line into a string variable which disables asp-like output formating. But yes, it is an option.

Reply

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Miha Markic

About me
Righthand
 
Microsoft MVP
 
Developer Express' DXSquad
INETA Country Leader for Slovenia
INETA Country Leader for Slovenia

Slovene Developer Users Group Lead
Friends of Red-Gate
LLBLGenPro Partner

Miha currently works as a free lance consultant and software developer specialized in .net area.
He graduated in Computer and information science at the University of Ljubljana, Slovenia. He has accumulated experience in various programming languages such as Java, Visual Basic 3-6 (MCP), Visual C++, Delphi, C# and VB.Net through years.
He has experience in practically all (technical) stages of project development, including planning, framework development, user interface, business processes, as well as testing and documenting. He has worked on big and small projects in Slovenia and abroad (e.g. participated in completing level 3 IS for the Nucor steel plant, Hertford, USA).
Currently he enjoys programming in .net environment using C#. Since 2000 he has been active in Developer Express' DX Squad and has been ECDL trainer and tester. He also gives lectures on conferences and other events in Slovenia.

Month List

Tag cloud

Most comments

Paulius Paulius
1 comments
us United States
Meh Meh
1 comments
us United States
bart dm bart dm
1 comments
nl Netherlands

RecentComments

Comment RSS