Click here to Skip to main content
14,243,279 members

String Enumerations in C#

Rate this:
4.47 (49 votes)
Please Sign up or sign in to vote.
4.47 (49 votes)
27 Jul 2005CPOL
Options for declaring and referencing static string data.


The idea of string enumerations in C# (and the lack thereof), has bothered me for a while. Enum's are useful things but only allow the storage of numeric types. What if you want to use strings?


It struck me that I had a number of requirements when using string 'constants'.

I wanted the ability to define the values in-line and reference them like an enum value - e.g. MyEnum.MyString. It would also be nice to be able to reference the string values as a particular enum type, for the purposes of validation and strong-typing.



The first thing I naturally gravitated towards was constants defined in a specific class. The following shows an example of this:

public sealed class MyConsts

    private MyConsts() {}

    public const string Val1 = "MyVal1";
    public const string Val2 = "MyVal2";
    public const string Val3 = "MyVal3";


The constants can be easily accessed as MyConsts.Val1 etc. This is extremely simple and reasonably neat. As these are constants the values are compiled in and as such can also be used nicely in switch statements.

Constants do have an interesting side effect in that the values are actually copied to any client code that uses the class. This means that client assemblies could potentially have out of date values if the values in the constants assembly were to change and it were redeployed without a client rebuild.

A possibly preferable alternative to the class would be to use a struct to house the constants, as this would always be created on the stack rather than the heap.

Static readonly

readonly values can be initialized with a value and changed only within a class's constructor. This effectively means their eventual value is not known at compile time. The code sample above can be changed slightly to illustrate this 'pseudo const'.

public sealed class MyConsts

    private MyConsts() {}

    public static readonly string Val1 = "MyVal1";
    public static readonly string Val2 = "MyVal2";
    public static readonly string Val3 = "MyVal3";

Values are again accessed in exactly the same way e.g. MyConsts.Val3, but as the values are conceptually dynamic, they will always be accessed from the class where they are defined. This does also mean that cannot be evaluated in switch statements.


Enum's can only contain numeric values right? Well, each value can actually be decorated with custom attributes, which means it's possible to extend a normal enum to contain string values as follows:

public enum HandTools
    [StringValue("Cordless Power Drill")
    Drill = 5,
    [StringValue("Long nose pliers")
    Pliers = 7,
    [StringValue("20mm Chisel")
    Chisel = 9


Enums can of course be declared without explicit numeric values, in which case each item will have implicit numeric items (starting from 0). This can be a bit of a pain when debugging (and provides a slight performance hit), so Microsoft suggests always declaring a numeric value.

The StringValue attribute is a simple attribute class (derived from System.Attribute) that is constructed with a string and exposes that string via a single Value property:

public class StringValueAttribute : System.Attribute

    private string _value;

    public StringValueAttribute(string value)
        _value = value;

    public string Value
    get { return _value; }


We now need some way of accessing these strings (via the StringValue attribute) and using them as part of our standard enum type.


The StringEnum class acts as a wrapper for string value access in enumerations. It assumes that enums wishing to expose string values do so via the StringValue attribute. The StringEnum class has static and instance portions and provides the following static methods:

  • Parse : Parse a string value and return the corresponding enum value.
  • GetStringValue : Return the string value associated with the given enum value.
  • IsStringValueDefined : Indicate the existence or non-existence of an enum value with the given string value.

The GetStringValue is shown below. This takes in an enum value and performs a fast type lookup before retrieving the StringValue attribute (if it can find it). The string value is then returned. We use a static Hashtable (_stringValues) to cache results (keyed by enum value) to avoid the minor reflection performance hit on subsequent requests.

public static string GetStringValue(Enum value)
    string output = null;
    Type type = value.GetType();

    //Check first in our cached results...
    if (_stringValues.ContainsKey(value))
      output = (_stringValues[value] as StringValueAttribute).Value;
        //Look for our 'StringValueAttribute' 
        //in the field's custom attributes
        FieldInfo fi = type.GetField(value.ToString());
        StringValueAttribute[] attrs = 
           fi.GetCustomAttributes(typeof (StringValueAttribute), 
                                   false) as StringValueAttribute[];
        if (attrs.Length > 0)
            _stringValues.Add(value, attrs[0]);
            output = attrs[0].Value;

    return output;

Case-insensitive overloads are provided for the Parse and IsStringValueDefined methods.

Similar instance methods are provided (using a constructor taking the enum's type), with an additional method for use in data binding:

  • GetListValues : Return an IList for data binding the StringEnum values.

Using the code

The code can be simply used by extending any enum to include [StringValue("")] attributes. You can then use the StringEnum class to retrieve the values as follows:

//Values intentionally unordered...

public enum CarType
    [StringValue("Saloon / Sedan")] Saloon = 5,
    [StringValue("Coupe")] Coupe = 4,
    [StringValue("Estate / Wagon")] Estate = 6,
    [StringValue("Hatchback")] Hatchback = 8,
    [StringValue("Utility")] Ute = 1,

public void TestMethod()
    //The line above shows 'Estate / Wagon'

                     "estate / wagon", true).ToString());
    //The line above converts back to an enum 
    //value from String Value (case insensitive)
    //and shows 'Estate'

    int enumValue = (int)StringEnum.Parse(typeof(CarType), 
                                     "estate / wagon", true);
    //The line above does the same again but this time shows 
    //the numeric value of the enum (6)

Points of interest

The source project has a full set of NUnit tests to illustrate each method, and the demo project shows other items such as examples of data binding with StringEnum.

Have fun!


  • (29/07/2005) - Updated source zip file with hashtable caching for GetStringValue.


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


About the Author

CodeBureau - Matt Simner
Software Developer (Senior) Codebureau
Australia Australia
Started with COBOL/CICS/DB2 - ended up with C#/ASP.NET. I'm a half-geek (or so my wife tells me!)

Comments and Discussions

GeneralRe: DescriptionAttribute Pin
CodeBureau - Matt Simner28-Jul-08 14:16
memberCodeBureau - Matt Simner28-Jul-08 14:16 
GeneralStatic Class option Pin
Edw22-Feb-08 8:22
memberEdw22-Feb-08 8:22 
GeneralTaking the static class one step further. Pin
James Coe10-Jun-08 10:57
memberJames Coe10-Jun-08 10:57 
QuestionHow I solved the same problem [modified] Pin
Azola73-Oct-07 5:33
memberAzola73-Oct-07 5:33 
AnswerRe: How I solved the same problem Pin
DarthOcellaris23-Feb-08 14:08
memberDarthOcellaris23-Feb-08 14:08 
GeneralAdded a small bit Pin
Shilshul13-Nov-06 7:04
memberShilshul13-Nov-06 7:04 
GeneralSlight optimization Pin
Keith Farmer27-Jul-05 8:43
memberKeith Farmer27-Jul-05 8:43 
GeneralRe: Slight optimization Pin
CodeBureau - Matt Simner27-Jul-05 12:24
memberCodeBureau - Matt Simner27-Jul-05 12:24 
Thanks Keith!

I certainly take the point about performance. I could easily introduce a level of caching for both static and instance methods. I guess I was trying to keep it simple for the moment, but that's a good next step I'll try to accommodate soon.

The underlying value omission is an oversight on my part. I think I was trying to illustrate that you could use the string enums as a name/value pair mechanism as well as an extended enum (with explicit values). I find the same thing annoying when hitting F12 to find an enum value only to find I've got to start counting from zero!

I'll try and incorporate both points in an update, and thanks again for the feedback.
GeneralRe: Slight optimization Pin
Keith Farmer27-Jul-05 12:32
memberKeith Farmer27-Jul-05 12:32 
GeneralRe: Slight optimization Pin
CodeBureau - Matt Simner27-Jul-05 12:37
memberCodeBureau - Matt Simner27-Jul-05 12:37 
GeneralRe: Slight optimization Pin
Kent Boogaart11-Aug-05 17:36
memberKent Boogaart11-Aug-05 17:36 
QuestionRe: Slight optimization Pin
Colin Bowern22-Feb-06 10:04
memberColin Bowern22-Feb-06 10:04 
AnswerRe: Slight optimization Pin
Keith Farmer22-Feb-06 11:30
memberKeith Farmer22-Feb-06 11:30 
Generalnice, but for simple things... Pin
Anonymous27-Jul-05 2:48
memberAnonymous27-Jul-05 2:48 
GeneralRe: nice, but for simple things... Pin
Paul Brower27-Jul-05 8:34
memberPaul Brower27-Jul-05 8:34 
GeneralRe: nice, but for simple things... Pin
Keith Farmer27-Jul-05 8:35
memberKeith Farmer27-Jul-05 8:35 
GeneralRe: nice, but for simple things... Pin
Robert Kozak27-Jul-05 13:47
memberRobert Kozak27-Jul-05 13:47 
GeneralRe: nice, but for simple things... Pin
CodeBureau - Matt Simner27-Jul-05 15:05
memberCodeBureau - Matt Simner27-Jul-05 15:05 

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.

Posted 27 Jul 2005


134 bookmarked