How to convert all elements from enum to string?
Assume I have:
public enum LogicOperands { None, Or, And, Custom }
And what I want to archive is something like:
string LogicOperandsStr = LogicOperands.ToString(); // expected result: "None,Or,And,Custom"
We can convert an enum to string by calling the ToString() method of an Enum.
The stringify() macro method is used to convert an enum into a string. Variable dereferencing and macro replacements are not necessary with this method. The important thing is that, only the text included in parenthesis may be converted using the stringify() method.
string s = string.Join(",",Enum.GetNames(typeof(LogicOperands)));
You have to do something like this:
var sbItems = new StringBuilder() foreach (var item in Enum.GetNames(typeof(LogicOperands))) { if(sbItems.Length>0) sbItems.Append(','); sbItems.Append(item); }
Or in Linq:
var list = Enum.GetNames(typeof(LogicOperands)).Aggregate((x,y) => x + "," + y);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With