Follow me on Twitter at Twitter.com/wbm
FYI, I'm blogging most of my stuff over at More Wally now.
You might want to add my rss feed to your reader at:http://morewally.com/cs/blogs/wallym/rss.aspx
Want the name of an Enum? - Wallace B. McClure

Wallace B. McClure

All About Wally McClure - The musings of Wallym on Web, HTML5, Mobile, MonoTouch for iPhone, MonoDroid for Android, and Windows Azure.

News

Personal Blog

Work Blog

.NET

Book Authors

Business

Family

Friends

Georgia Tech Bloggers

Personal

Archives

Want the name of an Enum?

I needed the name of my enum value yesterday.  How the heck do you get that?  I used the Enum.GetName() static method.  The specific call I did was:

Enum.GetName( typeof(enumname), enumname.value

If there are any other options for doing this, suggestions are always welcome. 

Posted: Apr 07 2007, 07:12 AM by Wallym | with 13 comment(s)
Filed under: ,

Comments

Ponnu said:

For an Enum like this

Enum Numbers

 One = 1

 Two = 2

 Three = 3

End Enum

won't Numbers.One.ToString() give the name?

Ponnu

# April 7, 2007 9:19 AM

Ayende Rahien said:

EnumName.EnumValue.ToString() ??

# April 7, 2007 11:53 AM

Sean said:

Doing a .ToSting() on the enum name will work although I believe it is deprecated in favor of the static Enum.GetName() method.

# April 7, 2007 1:47 PM

Jason said:

How about enumname.ToString() ?

# April 7, 2007 2:48 PM

Cyril Gupta said:

What is the behaviour of enum.tostring?

# April 7, 2007 3:17 PM

Stuart Ballard said:

Er.

ToString()?

# April 7, 2007 3:33 PM

Jens Christian Mikkelsen said:

Ahem, enumname.value.ToString()?

# April 7, 2007 4:10 PM

JJ said:

Enum.GetName is useful only if you want to get the name from the enum's ordinal value.

For ex. Enum.GetName(typeof(enumname), 0);

In other cases, you could simply use enumvalue.ToString();

# April 7, 2007 4:30 PM

jayson knight said:

On an instance of the enum in question, call ToString(). Of course that's assuming you have an instance. If not, GetName is the best option.

# April 7, 2007 6:13 PM

Wallym said:

Cool.  thanks everyone.  I thought that ToString on the enum value would give me a string representation of the value.

# April 7, 2007 10:34 PM

Chris Martin said:

@Sean

How the world could you deprecate ToString()?

# April 8, 2007 8:35 AM

Jason Haley said:

ToString() is most common ... but it won't work in all cases.  For example an enum that has multiple int values that are same:

public enum Numbers

   {

       First = 0,

       Zero = 0,

       One = 1,

       Two = 2,

       Three = 3,

       Last = 3

   }

This will not write out the expected "First" and "Zero":

  Console.WriteLine(Numbers.First.ToString());

  Console.WriteLine(Numbers.Zero.ToString());

It will be "First" and "First" ... you'll have to go to the Names to get the Zero as far as I know.

# April 8, 2007 11:03 AM

KNOCKS said:

GetNames is the way to go.

Anyone remembers how we achieved the same in C++?

# April 10, 2007 5:31 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)