1

Consider Below Code, how to let Console.WriteLine(Colors.All) output "Red, Yellow, Blue,Green", instead of "All"?

using System;

    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine(Colors.All);// All
                var noRed = Colors.All & ~Colors.Red;
                Console.WriteLine(noRed);// Yellow, Blue, Green
                Console.ReadLine();
            }
        }

        [Flags]
        enum Colors
        {
            None=0,
            Red= 1<<0,
            Yellow = 1<<1,
            Blue= 1<<2,
            Green=1<<3,
            All = Red | Yellow| Blue | Green
        }
    }
prime23
  • 3,362
  • 2
  • 36
  • 52
  • `enum` doesn't allow to override its `ToString()` (like in Java) so you have to write a helper class for it. I'd begin with `String.Join(", ", values)` where `values` holds old values you want to print. See http://stackoverflow.com/questions/479410/enum-tostring-with-user-friendly-strings – abatishchev Sep 09 '16 at 04:48
  • 1
    See also http://stackoverflow.com/questions/4171140/iterate-over-values-in-flags-enum – abatishchev Sep 09 '16 at 04:49
  • Generally: if the built-in enum-to-string behavior doesn't suit you, you need to implement an alternative yourself. See the marked duplicate for several options. In this particular case, if you code a variation of http://stackoverflow.com/questions/4171140/iterate-over-values-in-flags-enum that only returns individual flags that are set in an input value, you can use `string.Join(", ", ...)` to create the final string. – Peter Duniho Sep 09 '16 at 06:12
  • If you come up with a question that is actually appreciably different from any question already on Stack Overflow, post it with a good [mcve] that shows what you've tried, along with an explanation of what research you've done, what you found, and why that research did not lead you to a solution. – Peter Duniho Sep 09 '16 at 06:12

1 Answers1

0

To achieve desired result in your case, i think best approach is to use description attribute for your enum. view my code which will help you to achieve what you required.

[Flags]
    enum Colors
    {
        [Description("None")]
        None=0,
        [Description("Red")]
        Red= 1<<0,
        [Description("Burnt Yellow")]
        Yellow = 1<<1,
        [Description("Blue")]
        Blue= 1<<2,
        [Description("Green")]
        Green=1<<3,
        [Description("Red | Yellow| Blue | Green")]
        All = 99
    }

// Now create helper class and function to get enum description for associated value.

using System;
using System.ComponentModel;
using System.Reflection;


public static class EnumHelper
{
    /// <summary>
    /// Retrieve the description on the enum, e.g.
    /// [Description("Bright Pink")]
    /// BrightPink = 2,
    /// Then when you pass in the enum, it will retrieve the description
    /// </summary>
    /// <param name="en">The Enumeration</param>
    /// <returns>A string representing the friendly name</returns>
    public static string GetDescription(Enum en)
    {
        Type type = en.GetType();

        MemberInfo[] memInfo = type.GetMember(en.ToString());

        if (memInfo != null && memInfo.Length > 0)
        {
            object[] attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);

            if (attrs != null && attrs.Length > 0)
            {
                return ((DescriptionAttribute)attrs[0]).Description;
            }
        }

        return en.ToString();
    }

}



//Now use this function to get attribute for your enum value.
Console.WriteLine(EnumHelper.GetDescription(Colors.All));
iSensical
  • 747
  • 5
  • 8