CameTooFar

A Nerd's False Positive.

Non-numeric values for enumeration using Attributes in c#

In this post, I’ll explain how you can provide string values for an enum. Usually, we’ll  assign integer (by default), long or as a bit field using FlagAttribute.

For assigning a string value first you need to create a class inherited from Attribute class. 

image

Once your attribute class is ready. You can use the attribute and your string values as below: 

image

Now, for accessing the enum string value and converting the string values to respective enum , it’s good to have an extension so that you can convert back and forth seamlessly. 

image

Now, you are fully occupied for using the enum and its custom values. Below is an example on how to use it. 

image

image

 Hope this helps.

Thanks.