Bug 12825 - Mono and .net don't .ToString() an enum in the same way
Summary: Mono and .net don't .ToString() an enum in the same way
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib ()
Version: master
Hardware: PC Windows
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2013-06-21 14:22 UTC by Tristan
Modified: 2013-06-21 14:29 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Tristan 2013-06-21 14:22:07 UTC
Here’s some code:

namespace MonoTest
{
    class Program
    {
        public enum SomeValues
        {
            v1 = 1,
            v2 = 2,
            latest = v2,
        }

        static void Main(string[] args)
        {
            var v1AsString = SomeValues.v1.ToString();
            var v2AsString = SomeValues.v2.ToString();
            var latestAsString = SomeValues.latest.ToString();

            Console.WriteLine(v1AsString);
            Console.WriteLine(v2AsString);
            Console.WriteLine(latestAsString);

            Console.ReadKey();
        }
    }
}

Here’s what it outputs on .net/Windows:
v1
v2
v2

And on Mono/Android:
v1
latest
latest

Expected - same output.
Comment 1 Jonathan Pryor 2013-06-21 14:25:23 UTC
This is fixed in Mono 3.0.x, and should be fixed in Xamarin.Android 4.7.x.
Comment 2 Tristan 2013-06-21 14:29:46 UTC
Possibly not a bug!

My team mate (Clay) rightly points to:

http://msdn.microsoft.com/en-us/library/16c1xs4z.aspx it says

Notes to Callers
If multiple enumeration members have the same underlying value and you attempt to retrieve the string representation of an enumeration member's name based on its underlying value, your code should not make any assumptions about which name the method will return. For example, the following enumeration defines two members, Shade.Gray and Shade.Grey, that have the same underlying value.

enum Shade
{
    White = 0, Gray = 1, Grey = 1, Black = 2 
}

The following method call attempts to retrieve the name of a member of the Shade enumeration whose underlying value is 1. The method can return either "Gray" or "Grey", and your code should not make any assumptions about which string will be returned.

string shadeName = ((Shade) 1).ToString();

With your code latest and v2 are both assigned an integer value of 2, so the runtime is free to pick whichever one it feels like.