C # numeric value enum as string

I have the following listing:

public enum Urgency { VeryHigh = 1, High = 2, Routine = 4 } 

I can get the enumeration "value" as a string as follows:

 ((int)Urgency.Routine).ToString() // returns "4" 

Note. This is different from:

 Urgency.Routine.ToString() // returns "Routine" (int)Urgency.Routine // returns 4 

Is there a way to create an extension class or a static utliity class that will provide some syntactic sugar? :)

+41
enums c # return-type return-value
Aug 09 '10 at 22:33
source share
7 answers

You should simply use the overloads of the Enums ToString method to give it a format string, this will output the enumeration value as a string.

 public static class Program { static void Main(string[] args) { var val = Urgency.High; Console.WriteLine(val.ToString("D")); } } public enum Urgency { VeryHigh = 1, High = 2, Low = 4 } 
+73
Aug 09 '10 at 23:23
source share

In order to get more “readable” descriptions for the enumerations (for example, “Very High”, and not “VeryHigh” in your example), I decorated the enum values ​​with an attribute as follows:

 public enum MeasurementType { Each, [DisplayText("Lineal Metres")] LinealMetre, [DisplayText("Square Metres")] SquareMetre, [DisplayText("Cubic Metres")] CubicMetre, [DisplayText("Per 1000")] Per1000, Other } public class DisplayText : Attribute { public DisplayText(string Text) { this.text = Text; } private string text; public string Text { get { return text; } set { text = value; } } } 

Then the extension method is used as follows:

  public static string ToDescription(this Enum en) { Type type = en.GetType(); MemberInfo[] memInfo = type.GetMember(en.ToString()); if (memInfo != null && memInfo.Length > 0) { object[] attrs = memInfo[0].GetCustomAttributes( typeof(DisplayText), false); if (attrs != null && attrs.Length > 0) return ((DisplayText)attrs[0]).Text; } return en.ToString(); } 

Then you can just call

  myEnum.ToDescription () 
to display your listing as more readable text.
+17
Aug 09 2018-10-09T00:
source share

If you just want to process this enumeration, use the Mark Byer solution.

For a more general solution:

 public static string NumberString(this Enum enVal) { return Convert.ToDecimal(enVal).ToString("0"); } 

Converting to decimal means that you do not need to explicitly handle 8 different valid basic types of integrals, since they all convert losslessly to decimal, but not to each other (ulong and long do not convert losslessly among themselves, but both can handle to everyone else). Doing this is likely to be faster (especially if you go well in your comparison order), but much more verbose for a relatively small gain.

Edit:

The foregoing is not as good as Franrentosh, although Franrentosh saw the question to a real problem and solves it very eloquently.

+4
Aug 09 '10 at 23:20
source share

If you want, you can make an extension method for all enums:

 public static string ToValueString(this Enum enumValue) { if (enumValue.GetType().GetEnumUnderlyingType() == typeof(int)) return ((int)(object)enumValue).ToString(); else if (enumValue.GetType().GetEnumUnderlyingType() == typeof(byte)) return ((byte)(object)enumValue).ToString(); ... } 
+2
Aug 09 '10 at
source share

How about a little reflection? Should work with all basic types.

 public static class EnumTools { public static string ToRawValueString(this Enum e) { return e .GetType() .GetFields(BindingFlags.Public | BindingFlags.Static) .First(f => f.Name==e.ToString()) .GetRawConstantValue() .ToString(); } } 

Then:

 Console.WriteLine(Urgency.High.ToRawValueString()); //Writes "2" 
+2
Aug 09 '10 at
source share

Great stuff ... I added an extension method to my project

 public static class EnumExtensions { public static string NumberString(this Enum enVal) { return enVal.ToString("D"); } } 

Now I can get the int value - as a string - by calling Urgency.Routine.NumberString(); Thanks Frankentosh and Jon :)

+2
Aug 10 2018-10-10T00:
source share

You can write an extension method for your specific type:

 public static class UrgencyExtension { public static string ToIntegerString(this Urgency u) { return ((int)u).ToString(); } } 

Use the following:

 Urgency u = Urgency.Routine; string s = u.ToIntegerString(); 
+1
Aug 09 '10 at
source share



All Articles