Click here to Skip to main content
13,191,400 members (63,232 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


14 bookmarked
Posted 7 Jun 2014

Enum With Text Value in C#

, 7 Jun 2014
Rate this:
Please Sign up or sign in to vote.
Enum with text value in C#


In C#, we use enums frequently. Enum can only inherit from types byte, sbyte, short, ushort, int, uint, long, or ulong. But sometimes, we also may want to assign text value with it to extend its uses.


I have been working with Entity framework code first. And I had been using enums to hold information like:

  • UserType (SuperUser, AdminUser, … etc.)
  • Permission (UserManage, UnitManage, … etc.) and few more

It was going quite well. But a problem was introduced when I went to show the information based on enum values to the real user.

Real user doesn’t know what emun is. User type values like (0, 1, 2… etc.) or (SuperUser, AdminUser, … etc.) are confusing to them. Words like (Super User, Admin User, … etc.) seem quite suitable to them.

So I started to add some extra code using attributes and extinction methods, so that my emun could hold some little text description regarding it.

Using the Code

Here is the enum which will hold our text values. Here, we are using two custom attributes CustomEnum and TextValue which we are going to see in detail within moments.

[CustomEnum(true)]                           /*Important to use CustomEnum(true)*/
public enum PermissionEnum
    [TextValue("User setup manage")]        /*Holds the text value*/
    UserManage = 10,
    [TextValue("Unit setup manage")]
    UnitManage = 2,
    [TextValue("Exam setup manage")]
    ExamManage = 3,
    ExaminerManage = 1                     /*Its not necessary to use TextValue on all*/

Once we have created the enum, we will use it like this. Here, TextValue() is an extension method to get the assigned text.

/*Use of the custom enum*/
const PermissionEnum aPermission = PermissionEnum.UserManage;
String name = aPermission.ToString();                       //Shows the normal string value
String textValue = aPermission.TextValue();                 //Shows the assigned text value 
int value = (int)aPermission;                               //Shows the Value

Here are the two custom attributes, CustomEnum and TextValue that we mentioned earlier.

public class CustomEnumAttribute : Attribute
    public bool IsCustomEnum { get; set; }
    public CustomEnumAttribute(bool isCustomEnum) : this()
       IsCustomEnum = isCustomEnum;

    private CustomEnumAttribute()
       IsCustomEnum = false;
class TextValueAttribute : CustomEnumAttribute
    public String Value { get; set; }
    public TextValueAttribute(string textValue) : this()
        if (textValue == null)
            throw new NullReferenceException("Null not allowed in textValue at TextValue attribute");
        Value = textValue;

    private TextValueAttribute() : base(true)
        Value = string.Empty;

And last of all, we need the extension method for the enum which will help us to get the assigned text.

public static class CustomEnumUtility
    /// To use this extantion method, the enum need to have CustomEnumAttribute with CustomEnumAttribute(true)
    public static string TextValue(this Enum myEnum)
        string value = string.Empty;
        /*Check : if the myEnum is a custom enum*/
        var customEnumAttribute = (CustomEnumAttribute) myEnum
                                  .GetCustomAttributes(typeof(CustomEnumAttribute), false)

        if (customEnumAttribute == null)
            throw new Exception("The enum don't contain CustomEnumAttribute");
        else if (customEnumAttribute.IsCustomEnum == false)
            throw new Exception("The enum is not a custom enum");

        /*Get the TextValueAttribute*/
        var textValueAttribute = (TextValueAttribute)myEnum
                                     .GetCustomAttributes(typeof(TextValueAttribute), false)
        value = (textValueAttribute != null) ? textValueAttribute.Value : string.Empty;
        return value;

You may find the project in the attachment.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Bangladesh Bangladesh
No Biography provided

You may also be interested in...

Comments and Discussions

Question[My vote of 1] Good effort but... Pin
Suvabrata Roy8-Jun-14 21:55
professionalSuvabrata Roy8-Jun-14 21:55 
AnswerRe: [My vote of 1] Good effort but... Pin
diponsust9-Jun-14 7:12
memberdiponsust9-Jun-14 7:12 
GeneralRe: [My vote of 1] Good effort but... Pin
Suvabrata Roy9-Jun-14 18:26
professionalSuvabrata Roy9-Jun-14 18:26 
GeneralRe: [My vote of 1] Good effort but... Pin
diponsust10-Jun-14 1:45
memberdiponsust10-Jun-14 1:45 
GeneralRe: [My vote of 1] Good effort but... Pin
Suvabrata Roy10-Jun-14 2:20
professionalSuvabrata Roy10-Jun-14 2:20 
GeneralRe: [My vote of 1] Good effort but... Pin
DiponRoy19-Apr-15 11:37
memberDiponRoy19-Apr-15 11:37 
GeneralDescriptionAttribute Pin
PIEBALDconsult8-Jun-14 4:47
memberPIEBALDconsult8-Jun-14 4:47 
SuggestionWhy not DisplayName Pin
Dusan Paulovic7-Jun-14 19:37
memberDusan Paulovic7-Jun-14 19:37 
GeneralRe: Why not DisplayName Pin
F. Xaver10-Jun-14 0:15
memberF. Xaver10-Jun-14 0:15 
GeneralRe: Why not DisplayName Pin
Dusan Paulovic10-Jun-14 4:50
memberDusan Paulovic10-Jun-14 4:50 
GeneralMy vote of 1 Pin
Dusan Paulovic7-Jun-14 19:35
memberDusan Paulovic7-Jun-14 19:35 
GeneralRe: My vote of 1 Pin
DiponRoy19-Apr-15 11:28
memberDiponRoy19-Apr-15 11:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171017.1 | Last Updated 8 Jun 2014
Article Copyright 2014 by DiponRoy
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid