Convert Enum to String

Posted by Eric Weilnau on Stack Overflow See other posts from Stack Overflow or by Eric Weilnau
Published on 2009-01-27T15:15:40Z Indexed on 2012/09/26 21:38 UTC
Read the original article Hit count: 237

Filed under:

Which is the preferred way to convert an Enum to a String in .NET 3.5?

  • Enum.GetName
  • Enum.Format
  • toString

Why should I prefer one of these over the others? Does one perform better?

Justification for Accepted Answer

Based on the forum post in panesofglass answer, it appears that Microsoft indirectly endorses the following method of converting an enum value to a string.

Do not convert an enum value to a string using built-in enum methods.

...

This will cause problems when Dotfuscating. You should not use enum.ToString(), enum.GetNames(), enum.GetName(), enum.Format() or enum.Parse() to convert an enum to a string. Instead, use a switch statement, and also internationalize the names if necessary.

© Stack Overflow or respective owner

Related posts about .NET