How can I use enum in C# for storing string constants?

.NetStringConstantsEnums

.Net Problem Overview


> Possible Duplicate:
> Enum with strings

Is it possible to have string constants in enum like the following?

      enum{name1="hmmm" name2="bdidwe"}

If it is not, what is the best way to do so?

I tried it, but it's not working for string so right now I am grouping all related constants in one class like

      class operation
      {
          public const string  name1="hmmm";
          public const string  name2="bdidwe"
      }

.Net Solutions


Solution 1 - .Net

Enum constants can only be of ordinal types (int by default), so you can't have string constants in enums.

When I want something like a "string-based enum" I create a class to hold the constants like you did, except I make it a static class to prevent both unwanted instantiation and unwanted subclassing.

But if you don't want to use string as the type in method signatures and you prefer a safer, more restrictive type (like Operation), you can use the safe enum pattern:

public sealed class Operation
{
    public static readonly Operation Name1 = new Operation("Name1");
    public static readonly Operation Name2 = new Operation("Name2");

    private Operation(string value)
    {
        Value = value;
    }

    public string Value { get; private set; }
}

Solution 2 - .Net

You could do this using DescriptionAttribute, but then you'd have to write code to get the string out of the attribute.

public enum YourEnum
{
    [Description("YourName1")]
    Name1,

    [Description("YourName2")]
    Name2
}

Solution 3 - .Net

The whole point of enums is to be ordinal constants.
However, you can achieve what you want by using an extension method:

  enum Operation
  {
      name1,
      name2
  }

  static class OperationTextExtender
  {
        public static String AsText(this Operation operation)
        {
              switch(operation)
              {
                    case Operation.name1: return "hmmm";
                    case Operation.name2: return "bdidwe";
                    ...
              }
        }
  }

  ...
  var test1 = Operation.name1;
  var test2 = test1.AsText();   

Solution 4 - .Net

Your operation class won't compile as is... you didn't declare the type of name1 and name2...

But that is the approach I'd take... yes.

If you make it a struct then it becomes a value type which may or may not be what you wanted...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMaddy.ShikView Question on Stackoverflow
Solution 1 - .NetR. Martinho FernandesView Answer on Stackoverflow
Solution 2 - .NetTaylor LeeseView Answer on Stackoverflow
Solution 3 - .NetRobert GieseckeView Answer on Stackoverflow
Solution 4 - .NetJohn WeldonView Answer on Stackoverflow