user2650480
user2650480

Reputation: 479

Get a string that contains the integer representation of an enum value

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.

Upvotes: 1

Views: 260

Answers (1)

Jeppe Stig Nielsen
Jeppe Stig Nielsen

Reputation: 61982

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

Upvotes: 4

Related Questions