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.

6
  • Duplicate of stackoverflow.com/questions/8899498/… Commented Aug 4, 2013 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? Commented Aug 4, 2013 at 14:03
  • 1
    possible duplicate of Cast int to enum in C# Commented Aug 4, 2013 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. Commented Aug 4, 2013 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. Commented Aug 4, 2013 at 14:15

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.