Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8632749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:23:40+00:00 2026-06-12T09:23:40+00:00

What determines which name is selected when calling ToString() on an enum value which

  • 0

What determines which name is selected when calling ToString() on an enum value which has multiple corresponding names?

Long explanation of question follows below.

I have determined that this not determined uniquely by any of: alphabetical order; declaration order; nor, name length.

For example, consider that I want to have an enum where the numeric values correspond directly to a practical use, (e.g. rgb values for color).

public enum RgbColor 
{
    Black   = 0x000000,
    Red     = 0xff0000,
    Green   = 0x00ff00,
    Blue    = 0x0000ff,
    White   = 0xffffff
}

Now, with this enum, calling default(RgbColor) will return the rgb value for black. Let’s say I don’t want the default value to be black, because I want UI designers to be able to use a call to “Default” when they don’t have specific instructions about what color to use. For now, the Default value for UI designers to use is actually “Blue”, but that could change. So, I add an additional TextDefault definition on the enum, and now it looks like:

public enum RgbColorWithTextDefaultFirst
{
    TextDefault = 0x0000ff,
    Black   = 0x000000,
    Red     = 0xff0000,
    Green   = 0x00ff00,
    Blue    = 0x0000ff,
    White   = 0xffffff
}

Now, I have tested this and I find that calling RgbColorWithTextDefaultFirst.TextDefault.ToString() and RgbColorWithTextDefaultFirst.Blue.ToString() both yield “Blue”. So, I figured that the name that is declared last will overwrite the name of the previous declarations. To test my assumption, I wrote:

public enum RgbColorWithTextDefaultLast
{
    Black   = 0x000000,
    Red     = 0xff0000,
    Green   = 0x00ff00,
    Blue    = 0x0000ff,
    White   = 0xffffff,
    TextDefault = 0x0000ff 
}

However, to my surprise, RgbColorWithTextDefaultLast.Blue.ToString() and RgbColorWithTextDefaultLast.TextDefault.ToString(). My next guess is that it sorts the names by alphabetical order and returns the first one. To test this I try:

public enum RgbColorWithATextDefaultFirst
{
    ATextDefault = 0x0000ff,
    Black   = 0x000000,
    Red     = 0xff0000,
    Green   = 0x00ff00,
    Blue    = 0x0000ff,
    White   = 0xffffff
}

public enum RgbColorWithATextDefaultLast
{
    Black   = 0x000000,
    Red     = 0xff0000,
    Green   = 0x00ff00,
    Blue    = 0x0000ff,
    White   = 0xffffff,
    ATextDefault = 0x0000ff
}

Now, for all four of RgbColorWithATextDefaultFirst.ATextDefault.ToString(), RgbColorWithATextDefaultFirst.Blue.ToString(), RgbColorWithATextDefaultLast.ATextDefault.ToString(), RgbColorWithATextDefaultLast.Blue.ToString(), I end up with “Blue”. I realize that there is another distinguishing factor, which is length of the string. My guess is now that the selected name is determined by the length of the name string. So, my test is to use these declarations:

public enum RgbColorWithALast
{
    Black   = 0x000000,
    Red     = 0xff0000,
    Green   = 0x00ff00,
    Blue    = 0x0000ff,
    White   = 0xffffff,
    A = 0x0000ff
}

public enum RgbColorWithAFirst
{
    A = 0x0000ff,
    Black   = 0x000000,
    Red     = 0xff0000,
    Green   = 0x00ff00,
    Blue    = 0x0000ff,
    White   = 0xffffff
}

Now, guess what value I got for all of: RgbColorWithAFirst.A.ToString(); RgbColorWithAFirst.Blue.ToString(); RgbColorWithALast.A.ToString(), RgbColorWithALast.Blue.ToString(). That’s right, “Blue”.

At this point, I’ve given up on trying to figure out what determines this by guessing. I opened up reflector, and I’m going to take a look and try to figure this out, but I figured I would ask a question here to see if anyone here already knows the answer, which is, again: What determines which name is selected when calling ToString() on an enum value which has multiple corresponding names?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T09:23:41+00:00Added an answer on June 12, 2026 at 9:23 am

    I might be going too far here, but I think it’s decided by binary search of the sorted values, and so can depend on the parity of the total number of values. You can illustrate this with your last example (RgbColorWithAFirst and RgbColorWithALast) by defining another value in both – then you get A from all the ToString invocations.

    I got here by decompiling mscorlib (4.0) and noting that eventually we get to a call to Array.BinarySearch on a sorted array of the declared values. Naturally, the binary search stops as soon as it gets a match, so to get it to switch between two identical values the easiest way is to alter the search tree, by adding an extra value.

    Of course, this is an implementation detail and should not be relied on. It seems to me that in your case you would be best served by using DescriptionAttribute on enum values where you want to be explicit about the display value, and a helper method such as:

    public static class EnumExtensions
    {
        public static string Description(this Enum value)
        {
            var field = value.GetType().GetField(value.ToString());
            var attribute = Attribute.GetCustomAttribute(
                                field, 
                                typeof (DescriptionAttribute)) 
                            as DescriptionAttribute;
    
            return attribute == null ? value.ToString() : attribute.Description;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So the first question is how does dispatch_async determines which thread to use? just
When implementing the Strategy Pattern, where does one put the code that determines which
I have a combobox in winforms, which, depending on what you select, determines what's
How does Castle Windsor determine which constructor to resolve when there are multiple constructors
I have Listbox which is bound to my view model. This view model has
I'm trying to write a perl script that determines which users are currently logged
I have a scenario in which there are multiple iframes/frames open in one web
Problem: Determine which radio button was sent based on their order and submit the
I want to determine which part of audio file contain speech or music. I
How do we determine which user the php script is running under when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.