Search code examples
c#enumsexamine

How to retrieve an Enums value as a string in C#


I have the following enum:

public enum DocumentState
{
    Default = 0,
    Draft = 1,
    Archived = 2,
    Deleted = 3
}

Most places in my solution I use it as a normal enum. Some places I use at an int casting it like this:

(int)DocumentState.Default)

But then, some places, as for example when I work with Examine (who only accepts strings, and not ints as inputs), I need to pass on the enumns int value as if it was a string. This can be done in the following way:

((int)DocumentState.Default).ToString()

My question is now; can it really be true that there is no other way to retrieve the enums value as a string?

I know that I might be abusing the Enum, but sometimes this is the best approach in the given case.


Solution

  • Use DocumentState.Default.ToString("d"). See https://msdn.microsoft.com/en-us/library/a0h36syw(v=vs.110).aspx