1
string userTypeId = ((int)ERPSystemUserType.Basic).ToString();

public enum ERPSystemUserType
{
    Basic = 20,
    Upgraded = 30
}

if I use ToString(), return the enum type's string value, however I want to use the defined int value.

Is there any attribute or way, I could use for return string by is number of the enum? Instead of doing ((int)ERPSystemUserType.Basic).ToString(). And don't want to use the Extension, then I have to use to every enum.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
user2650480
  • 429
  • 2
  • 5
  • 17
  • Duplicate of http://stackoverflow.com/questions/8899498/is-it-possible-to-cast-enum-to-integer – Venson Aug 04 '13 at 14:02
  • 2
    No, there is no method for a single value, just cast it to `int`. But why do you need it as string at all? – Tim Schmelter Aug 04 '13 at 14:03
  • 1
    possible duplicate of [Cast int to enum in C#](http://stackoverflow.com/questions/29482/cast-int-to-enum-in-c-sharp) – marc_s Aug 04 '13 at 14:06
  • 1
    It is not a duplicate of the threads linked. The asker wants a string `"20"`. There's no need to go through an `int`, explicitly. – Jeppe Stig Nielsen Aug 04 '13 at 14:13
  • There may not be a need but it seems more semantically correct to me to cast to an int and then do ToString on the result. – Jeff Aug 04 '13 at 14:15
  • After a lot of searching, I did find an older thread, actually: [C# enum value as string](http://stackoverflow.com/questions/3444699/c-sharp-enum-value-as-string). – Jeppe Stig Nielsen Aug 04 '13 at 14:46

1 Answers1

4

Use .ToString("D"). See Enumeration Format Strings.

Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181