I was implementing a C++ DLL wrapper in a C# application today and I used quite some enumerations. For debugging purposes it's always nice to print out a string or a description of the enumeration value instead of 0, 1, 252 or DEVICETYPEFOO. The problem is that we can't override ToString(). Luckily for us functionality to achieve this can be created quite easily and very generic so we don't need to create large functions per enumeration type filled with switch-case statements or if-clauses.
We start off by creating a very simple and straightforward class which will hold the description of the enumeration value.
This allows us to add the Description attribute to the enumeration values. If we don't specify the description attribute the ToString() function will be used. In this case it will print out GRANNYSMITH or JONATHAN.
The last thing we need is a function which will convert the enumeration value to the corresponding description. Don't forget to add a using statement for the System.Reflection namespace.
All we need to do is call the GetEnumDescription function.