Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Enum UNDERLYING Integer value toString

Tags:

c#

enums

casting

Trying to get the underlying Integer value for my enum returned as a String.

Tried

return ((int) MyEnumValue).ToString;

But fails with

Error 1 Cannot convert method group 'ToString' to non-delegate type 'string'. Did you intend to invoke the method?

like image 345
Maxim Gershkovich Avatar asked Mar 07 '11 06:03

Maxim Gershkovich


People also ask

Can enum be converted to string?

We can convert an enum to string by calling the ToString() method of an Enum.

What happens if you toString an enum?

ToString(String)Converts the value of this instance to its equivalent string representation using the specified format.

Can you convert enum to string C++?

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. Let's demonstrate the code implementation.

Can we override toString method in enum?

The toString() method of Enum class returns the name of this enum constant, as the declaration contains. The toString() method can be overridden, although it's not essential.


1 Answers

The less parentheses option is:

return MyEnumValue.ToString("d");
like image 144
bkaid Avatar answered Oct 14 '22 20:10

bkaid