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

The Archive Base Latest Questions

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

How can I group Enum values? Assume I have an enum like public enum

  • 0

How can I group Enum values?
Assume I have an enum like

public enum Colors
  {
    LightBlue,
    LightGreen,
    DarkGreen,
    Black,
    White,
    LightGry,
    Yellow
  }

Now I want to define some groups of colors, e.g. the light colors (LightBlue, LightGreen, White, LightGray, Yellow) and dark colors (Black, DarkGreen).
So I can ask for groups at different places in my code.
If I remember correctly my Java time I just could add methods to the enums in Java. I think that is not possible in C#. But maybe there are other ways.

Edit1: Of course I can add a Utility class with static member like IsADarkColor(Colors c). But I would like do it without an additional class because I could forget that related class when I need that feature.

  • 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-30T02:03:03+00:00Added an answer on May 30, 2026 at 2:03 am

    I can add a Utility class with static member like IsADarkColor(Colors c). But I would like do it without an additional class because I could forget that related class when I need that feature.

    This is when Extension Methods come in handy:

    // Taking Reed Copsey's naming advice
    public enum Color
    {
        LightBlue,
        LightGreen,
        DarkGreen,
        Black,
        White,
        LightGray,
        Yellow
    }
    
    public static class Colors
    {
        public static bool IsLightColor(this Color color)
        {
            switch(color){
                case Color.LightBlue:
                case Color.LightGreen:
                case Color.DarkGreen:
                case Color.LightGray:
                return true;
                default: 
                return false;
            }
        }
    }
    

    As long as these two classes are in the same namespace, you can see the static method as if it belonged to the Color class:

    var color = Color.LightBlue;
    if(color.IsLightColor()) {...}
    

    (hat tip to @Abdul for making me think of extension methods)

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

Sidebar

Related Questions

I have a Python regular expression that contains a group which can occur zero
Perhap this is a simple basic question Having an enum public enum TK{ ID,GROUP,DATA,FAIL;
I am trying to group bunch of bracts in java enum . Can some
Trying to work with groupby so that I can group together files that were
How can I using group by with union in T-SQL? I want to group
how can you do multiple group by's in linq to sql? Can you please
Can I apply clipview to a vertical expanding Group? Time to time I'm appending
How can I display more than 1 display group WITHIN a div? I simply
Given : Group hasMany Persons but the relationship is independent (ie. Persons can exist
Everything I can find in linq for aggregation has a group by clause. How

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.