. Topicala Page Index Token

A Journal about the experiences I have developing little applications in C#, Perl, Html and Javascript and talking about things new things that I use. Always Geeky; Always Nerdy; Always poor Grammer!

I am a Software Analyst Developer working in Southport, England but living in Liverpool. I develop mainly in C# and ASP.Net. I have been developing comercial software for several years now. I maintain this site (hosted at SwitchMedia UK) as a way of exploring new technologies (such as AJAX) and just generally talking about techie geek issues. This site is developed through a host of Perl scripts and a liberal use of Javascript. I enjoy experimenting with new technologies and anything that I make I host here.

Quick Search

Web www.kinlan.co.uk

Monday, October 24, 2005

RE: C# : Where do you define an enum

There is an interesting article over on Abhinaba's weblog about where you would place the declaration of the enum. I personally don't agree with his argument. He says that his ideal way of defining the placement of an enum is at the level of the class and not inside the class.

His basic argument that typing the class name infront of the enum can become a pain. I don't really care that it can be a pain, that is what intelisense is there for. Having the enum defined in the class is better in my opinion because it means that there will be fewer abiguity problems if you need to use another enum with the same name from another class. It is, in my opinion also easier to read.

Anyway, below is a snippet of his journal entry. To see what he has to say visit his blog, and see if you agree with me or not.

Email me: paul.kinlan@gmail.com

Frequently while designing classes that have methods which accept enums as parameters, a common question arrises on where to define the enum. Whether to define it inside the class or in the same level as the class.


Lets consider a class Folder which has a method List. It accepts a enum Filter and based on it prints the name of all files or directories in the Folder. We can define the enum at the same level as the Folder as below

enum Filter

{

File,

Dir

}

class Folder

{

public Folder(string path) { /* ... */ }

public void List(Filter filter) { /* ... */ }

}

Folder folder = new Folder("c:\");

folder.List(Filter.File);


Or define it inside the Folder class as in

class Folder

{

public enum Filter

{

File,

Dir

}

public Folder(string path) { /* ... */ }

public void List(Filter filter) { /* ... */ }

}

 

Folder folder = new Folder(@"c:\");

folder.List(Folder.Filter.File);

...


[Via MSDN Blogs]

Technorati Tags
[feed], [feed], [feed], [feed]

Comments: [Add New]

You shouldnt use two enum types of the same name in the same namespace.

Its bad practice!

By Anonymous, at Thursday, March 29, 2007 12:06:00 PM

Thanks for the comment, but I don't belive that is what I was saying in the post.

However, I am moving away from my original argument in this post. I don't belive nested enums make that much sense anymore.

By Paul Kinlan, at Thursday, March 29, 2007 12:24:00 PM