I know the following is not possible because it has to be an int
enum GroupTypes
{
TheGroup = "OEM",
TheOtherGroup = "CMB"
}
From my database I get a field with incomprehensive codes (the OEM and CMB's). I would want to make this field into an enum or something else understandable. Because the target is readability the solution should be terse.
What other options do I have?
Here is the extension method that I used to get the enum value as string. First here is the enum.
The Description attribute came from System.ComponentModel.
And here is my extension method:
Now, you can access the enum as string value using the following code:
enums in C# are restricted to underlying integer numeric types (byte, sbyte, short, ushort, int, uint, long, and ulong). You can't associate them with a character or string based underlying value.
A different approach might be to define a dictionary of type Dictionary<string, string>.
I've done something like this;
Call it with this;
Use a class.
Edit: Better example
A small tweak to Glennular Extension method, so you could use the extension on other things than just ENUM's;
Or Using Linq
Based in other opinions, this is what I come up with. This approach avoids having to type .Value where you want to get the constant value.
I have a base class for all string enums like this:
The JsonConverter is like this:
And an actual string enum will be something like this:
And with this, you can just use Color.Red to even serialize to json without using the Value property