I have an enum
private enum TimeUnit
{
Day,
Month,
Year
}
And I'm populating a description with:
return string.Concat(unit, "(s)");
Where unit is a TimeUnit. Most of the time this works fine and displays "Days(s)" however on a particular server it's displaying as "1(s)"
What would cause this?
The Java Enum has two methods that retrieve that value of an enum constant, name() and . toString(). The toString() method calls the name() method which returns the string representation of the enum constant.
Get the value of an Enum To get the value of enum we can simply typecast it to its type. In the first example, the default type is int so we have to typecast it to int. Also, we can get the string value of that enum by using the ToString() method as below.
We can convert an enum to string by calling the ToString() method of an Enum.
Enum. GetName(Type, Object) Method is used to get the name of the constant in the specified enumeration that has the specified value. Syntax: public static string GetName (Type enumType, object value);
Try using Enum.GetName()
it also has the advantage of being safer since it requires:
You should format appropriately using ToString
:
return string.Concat(unit.ToString("F"), "(s)");
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