It is very strange that the value in the debugger is different from the value of ToString . According to the documentation , they must match (because the Enum type really overrides ToString ).
If the C # object has an overridden ToString() , the debugger will call the override and display its result instead of the standard {<typeName>} .
Obviously this does not work for listings. My best guess is that the debugger is trying to do special, undocumented handling of enum types. Adding DebuggerDisplayAttribute seems to fix the problem by overriding this behavior.
[DebuggerDisplay("{ToString()}")] [Flags] public enum Categories : uint { ... }
Categories .F2.ToString () = "F, F2"
C # will not do this magic for you, because F2 already has its own name in the listing. You can manually mark individual items as follows:
public enum Categories { [Description("F, F2")] F2 = F | (1 << 24), }
And then write code to convert to a description.
public static string ToDescription(this Categories c) { var field = typeof(Categories).GetField(c.ToString()); if (field != null) { return field.GetCustomAttributes().Cast<DescriptionAttribute>().First().Description; } } ... Categories.F2.ToDescription() == "F, F2";
Or you could do some magic to generate this yourself:
public static string ToDescription(this Categories c) { var categoryNames = from v in Enum.GetValues(typeof(Categories)).Cast<Category>() where v & c == c orderby v select v.ToString(); return String.Join(", ", categoryNames); }
Unfortunately, the extension method cannot be used with DebuggerDisplayAttribute , but you can use DebuggerTypeAttribute , YMMV, but you can try the following:
[DebuggerType("CategoryDebugView")] [Flags] public enum Categories : uint { ... } internal class CategoryDebugView { private Category value; public CategoryDebugView(Category value) { this.value = value; } public override string ToString() { var categoryNames = from v in Enum.GetValues(typeof(Categories)).Cast<Category>() where v & c == c orderby v select v.ToString(); return String.Join(", ", categoryNames); } }