Consider the following enum:
[Flags]
public enum MyEnum
{
Option1 = 0,
Option2 = 1,
Option3 = 2,
Option4 = 4
}
Is there a way in which I can get the string equivalent (not the value) as a CSV string? Normally I can use the Enum.GetName function to convert a type to the string representation, however, if you use a combination this returns null.
So basically I would like to convert:
var options = MyEnum.Option1 | MyEnum.Option3;
into
"Option1, Option3"
Then I would like to be able to convert “Option1, Option3” back to MyEnum.Option1 | MyEnum.Option2.
Suggestions?
Well, aside from
Option1not making much sense in a flags enum, this just works by default usingEnum.ParseandEnum.ToString().Start with this:
Then you can always do this:
Now try this:
Which seems like it does exactly what you wanted (again, ignoring the fact that Option1 in your example will never occur)
docs: Enum.Parse