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

  • SEARCH
  • Home
  • 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 8996591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:46:29+00:00 2026-06-15T23:46:29+00:00

I have multiple flags enums defined in code similar to the following [Flags] public

  • 0

I have multiple flags enums defined in code similar to the following

[Flags]
public enum Colors
{
   None = 0,
   Red = 1,
   Green = 2,
   Blue = 4, 
   Purple = Red | Blue,
   Brown = Red | Green,
}

The following code produces the following output

Colors color1 = Colors.Red | Colors.Blue;
Colors color2 = Colors.Purple;
string s1 = color1.ToString(); // Sets s1 to "Purple"
string s2 = color2.ToString(); // Sets s2 to "Purple"

I want a method that outputs the individual bits of a bitwise enum, even if a matching combination is defined.

private void Foo()
{
  Colors color1 = Colors.Red | Colors.Blue;
  Colors color2 = Colors.Purple;
  string s1 = CreateColumnString(color1); // Sets s1 to "Red|Blue"
  string s2 = CreateColumnString(color2); // Sets s2 to "Red|Blue"
}

I thought I could loop through all the values of an enum and check if the value is a power of two. But I can’t figure out how to get the underlying value of the Enum argument.

private string CreateColumnString(object value)
{
 //is this an enum with Flags attribute?
 if (value is Enum  && value.GetType().GetCustomAttributes(typeof(FlagsAttribute), true).Length > 0)
 {
    Enum e = (Enum)value;
    //Get a list of Enum values set in this flags enum
    IEnumerable<Enum> setValues = 
      Enum.GetValues(value.GetType())
          .Cast<Enum>()
          .Where(eachEnum => IsPowerOfTwo(eachEnum) && value.HasFlag(eachEnum)); 

    return string.Join("|", setValues);
 }
 else
 {
    return value != null ? value.ToString() : string.Empty;
 }
 return str;
}

private static bool IsPowerOfTwo(Enum e)
{
   int x = (int)e; //ERROR cannot convert type 'System.Enum' to 'ulong'
   return (x != 0) && ((x & (x - 1)) == 0);
}
  • 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-15T23:46:30+00:00Added an answer on June 15, 2026 at 11:46 pm

    Here’s another approach. I figure the more options you have, the better 🙂

    public static class EnumHelpers
    {
        public static string ToStringExtended<T>(this Enum e)
        {
            if (!(e.GetType().GetCustomAttributes(typeof(FlagsAttribute), true).Length > 0))
                return e.ToString();
    
            List<string> eNames = new List<string>();
            foreach (T fish in Enum.GetValues(typeof(T)))
            {
                Enum num = fish as Enum;
                if (e.HasFlag(num) && Convert.ToInt32(fish) != 0 && Convert.ToInt32(fish) != Convert.ToInt32(e))
                    eNames.Add(fish.ToString());
            }
    
            return eNames.Count > 1 ? String.Join("|", eNames.ToArray()) : e.ToString();
        }
    }
    

    The usage is almost identical to what Fredirk proposed:

    Colors c = Colors.Purple;
    Console.WriteLine(c.ToStringExtended<Colors>());
    // Output : Red|Blue
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following enum: [Flags] public enum PostAssociations { None = 0x0, User
For instance. I have the following enum [Flags] public enum Stuff { stuff1=1, stuff2=2,
Assume i have an enumeration : namespace System.Windows.Forms { public enum DialogResult { None,
I have multiple ajax requests with javascript code as response, and I need to
I have a int variable which hold multiple flags, for instance: int styles =
I am using Drupal 6 and have multiple flags on my nodes each with
So in C++/C# you can create flags enums to hold multiple values, and storing
I have an enum declaration using bit flags and I cant exactly figure out
If I have an enum with multiple values which can be present at the
I'm having difficulties working with some legacy enums that have multiple zero values. Whenever

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.