Quantcast
Channel: CameTooFar
Viewing all articles
Browse latest Browse all 45

Non-numeric values for enumeration using Attributes in c#

$
0
0

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.


Viewing all articles
Browse latest Browse all 45

Trending Articles