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 904351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:02:03+00:00 2026-05-15T16:02:03+00:00

I came up with this piece of code that converts the set flags in

  • 0

I came up with this piece of code that converts the set flags in a variable of type Flag Enumeration and returns the set flags as integers.
I’d like to know if this is the best approach.

Example enumeration:

[Flags]
enum Status {
  None = 0x0,
  Active = 0x1,
  Inactive = 0x2,
  Canceled = 0x4,
  Suspended = 0x8
}

The extension method that converts the set flags to array of int that I came up with:

public static class Extensions
{
    public static int[] ToIntArray(this System.Enum o) 
    {
        return o.ToString()
            .Split(new string[] { ", " }, StringSplitOptions.None)
            .Select(i => (int)Enum.Parse(o.GetType(), i))
            .ToArray();
    }
}

This is how I use it:

Status filterStatus = Status.Suspended | Status.Canceled;

int[] filterFlags = filterStatus.toIntArray();

foreach (int flag in filterFlags) {
   Console.WriteLine("{0}\n", flag);
}

It will output:

4
8

As you can see, to get this done I'm doing the following:

  1. Converting the variable to string. It outputs something like: Suspended, Canceled
  2. Splitting that string into an array of strings: { "Suspended", "Canceled" }
  3. Converting that string to the enumeration value with Enum.Parse.
  4. Casting the value to an integer.
  5. Converting the IEnumerable to int[].

It works, but I just don't think it's the best approach. Any suggestions to improve this bit of code?

  • 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-05-15T16:02:04+00:00Added an answer on May 15, 2026 at 4:02 pm

    To keep it linq-like

    var flags = Enum.GetValues(typeof(Status))
                    .Cast<int>()
                    .Where(f=> f & o == f)
                    .ToList();
    

    One gotcha with this approach is that it will include aggregate enumeration values. For example:

    [Flags]
    public enum Status
    {
        None = 0,
        One = 1,
        Two = 2,
        All = One | Two,
    }
    
    var flags = Enum.GetValues(typeof(Status))
                    .Cast<int>()
                    .Where(f=> f & o == f)
                    .ToList();
    

    Here flags will have 1, 2, 3, not just 1, 2.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I came across a piece of code which looks like this: jQuery(function($) { $('#saySomething').click(function()
I came across a piece of code that is: for(i=((90.0E1,0x5A)<=(0x158,140.70E1)?(.28,3.45E2,0):(95.30E1,26.40E1)<=1.400E2?(1,this):(108.,0x227));i<length;i++) { // some other
I came across this piece of code that's supposed to ensure that only one
I have a piece of code that looks like this: Algorithm a = null;
Recently, I came across the following piece of code in perl that returns the
I was look at somebody else's code and came across this piece of code
I came across this pretty little piece of code here : List<DataTable> result =
As part of answering another question, I came across a piece of code like
Please take a look at this piece of code I came up with. abstract
just wondering if someone else came across this. I got this piece of code

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.