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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:58:10+00:00 2026-05-22T18:58:10+00:00

I like to write enum or integer to pass option to my methods. Is

  • 0

I like to write enum or integer to pass option to my methods. Is there a pattern or a method in C# to check if an (int 1,2,4,8,…) option is true or false. I think it should easily be possible via binary functions.

class Program
{
    public enum Option
    {
        Option_A = 1,
        Option_B = 2,
        Option_C = 4,
        Option_D = 8,
    }

    static void Main(string[] args)
    {
        int activeOption = 5; // Means I activeted the Option_A and Option_C
        if (IsOption(activeOption, Option.Option_A)) { /*do work*/ }
        if (IsOption(activeOption, Option.Option_B)) { /*do work*/ }
        if (IsOption(activeOption, Option.Option_C)) { /*do work*/ }
        if (IsOption(activeOption, Option.Option_D)) { /*do work*/ }
    }

    private static bool IsOption(int activeOption, Option option)
    {
        /*Evaluate if IsOption is true or false*/
        throw new NotImplementedException();
    }
}

EDIT

Am I limited the number of options that I can create like this?

  • 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-22T18:58:11+00:00Added an answer on May 22, 2026 at 6:58 pm

    Since your enum contains flags (or if you prefer, is a bitfield), you should add a FlagsAttribute to it:

    [Flags]
    public enum Option
    {
        Option_A = 1,
        Option_B = 2,
        Option_C = 4,
        Option_D = 8,
    }
    

    And then, checking is typically done with the bitwise and operator. A cast will also be needed, because you are using an int variable.

    if(((Option)activeOption & Option.Option_A) != Option.Option_A) //...
    

    If you want to encapsulate this nastiness away, check out the article linked in Smudge202’s answer. If you are running .NET 4, you don’t even need to do that: check sehe’s answer.

    But you should really try using a variable of type Option directly, and combine the options with the bitwise or operator:

    Option activeOption = Option.Option_A | Option.Option_C;
    

    Of course using this scheme limits the number of options you can create. If you leave it as is, you can only create 32 different options, because an int (the default underlying type of an enum) has only 32-bits. If you use a long you can have 64 different options:

    [Flags]
    public enum Option : long
    {
        Option_A = 1,
        Option_B = 2,
        Option_C = 4,
        Option_D = 8,
        // blah blah
    }
    

    However, if you need an arbitrary number of options, it’s probably time to change strategies. You could make a custom type that behaves like an enum, but you’ll probably be better off with just a regular, non-flags enum, and a HashSet<Option>.

    public enum Option
    {
        Option_A = 1, // notice the sequential values now
        Option_B = 2,
        Option_C = 3,
        Option_D = 4,
    }
    
    HashSet<Option> options = new HashSet<Option> { Option.Option_A, Option.Option_C };
    if(options.Contains(Option.Option_A)) // ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to write an extension method for string, which appears as a static
Part 1 In C, is there any difference between declaring an enum like this:
Would you write something like: enum XYZ_TYPE {X=1, Y=2, Z=3}; I saw it and
I'd like to write a safe version of toEnum : safeToEnum :: (Enum t,
I'd like to write an extension method that extends some member of my class.
I'd like to write a game for the Nintendo Wii. How do I go
I'd like to write a script/batch that will bunch up my daily IIS logs
I would like to write a small program in C# which goes through my
I would like to write some data to a file in Ruby. What is
I would like to write a plug-in that will allow a custom written CRM

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.