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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:09:12+00:00 2026-05-28T05:09:12+00:00

I have an enum, suppose it is called sports: enum Sports { Baseball =

  • 0

I have an enum, suppose it is called sports:

enum Sports
{
  Baseball = 1,
  Basketball = 2,
  Football = 4,
  Hockey = 8,
  Soccer = 16,
  Tennis = 32,
  etc...
}

I would basically like to add an extension method that clears a mask bit like this:

Sports Mask = Sports.Baseball | Sports.Football | Sports.Tennis; //37
Mask.Clear(Sports.Baseball);
// Mask = Football | Tennis

This is the extension method I’ve come up with, it doesn’t work. Clear doesn’t affect Mask, the value remains 37. I’m not sure I can modify the this portion from within the extension method. Is there some other way to accomplish this?

public static void Clear<T>(this Enum value, T remove)
{
  Type type = value.GetType();
  object result;

  if (type.Equals(typeof(ulong)))
  {
    result = Convert.ToUInt64(value) & ~Convert.ToUInt64((object)remove);
  }
  else
  {
    result = Convert.ToInt64(value) & ~Convert.ToInt64((object)remove);
  }

  value = (Enum)Enum.Parse(type, result.ToString());
}
  • 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-28T05:09:12+00:00Added an answer on May 28, 2026 at 5:09 am

    Because enums are value types, so that you modified the parameter only. You can return the modified value and assign it to the original variable.

    public static T Clear<T>(this Enum value, T remove) { ... }
    mask = mask.Clear(Sports.Baseball);
    //just like
    //DateTime dt = DateTime.Now;
    //dt = dt.AddHour(1);
    

    And by the way, I don’t think it worth an extension method here. Why not just simply use:

    mask = mask & ~Sports.BaseBall;
    mask = mask & ~anotherMask;
    

    Your extension method is very inefficient because of (un)boxing.

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

Sidebar

Related Questions

Suppose I have an enum with arbitrary increments between contents. Like so: typedef enum
Sorry for the very noob question. Let's suppose I have an enum like so
Possible Duplicate: How to enumerate an enum? Suppose that i have an enumeration like
Suppose we have an enum like the following: enum Days {Saturday, Sunday, Tuesday, Wednesday,
Suppose I have an enum like data T = A | B | C
Suppose I have this enum: public enum TestEnum { EXAMPLE, FURTHER_EXAMPLE, LAST_EXAMPLE } With
Suppose I have the following: typedef enum functionType {ln, sin, sqrt} functionType; NSArray *functions
I have enum like this [Flags] public enum Key { None = 0, A
I have enum like this: public enum ObectTypes { TypeOne, TypeTwo, TypeThree, ... TypeTwenty
Suppose I have a data type enum TreeTypes { TallTree, ShortTree, MediumTree } .

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.