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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:01:20+00:00 2026-05-13T12:01:20+00:00

It recently occured to me that the following (sample) enumeration… enum Color { Red,

  • 0

It recently occured to me that the following (sample) enumeration…

enum Color
{
    Red,
    Green,
    Yellow,
    Blue
}

… could be replaced with a seemingly more type-safe class:

class Color
{
    private Color() { }

    public static readonly Color   Red      = new Color();
    public static readonly Color   Green    = new Color();
    public static readonly Color   Yellow   = new Color();
    public static readonly Color   Blue     = new Color();
}

With “type-safe”, I mean that the following statement would work if Color was an enum, but not if Color were the above class:

var nonsenseColor = (Color)17;    // works if Color is an enum

Two questions:

1) Is there a widely accepted name to this pattern (replacing an enum with a type-safe class)?

2) In which cases should one use enums, and when would a class be more appropriate?

  • 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-13T12:01:20+00:00Added an answer on May 13, 2026 at 12:01 pm

    Enums are great for lightweight state information. For example, your color enum (excluding blue) would be good for querying the state of a traffic light. The true color along with the whole concept of color and all its baggage (alpha, color space, etc) don’t matter, just which state is the light in. Also, changing your enum a little to represent the state of the traffic light:

    [Flags()]
    public enum LightColors
    {
        unknown = 0,
        red = 1,
        yellow = 2,
        green = 4,
        green_arrow = 8
    }
    

    The current light state could be set as:

    LightColors c = LightColors.red | LightColors.green_arrow;
    

    And queried as:

    if ((c & LightColors.red) == LightColors.red)
    {
        //Don't drive
    }
    else if ((c & LightColors.green_arrow) == LightColors.green_arrow)
    {
        //Turn
    }
    

    Static class color members would be able to support this multiple state without extra functionality.

    However, static class members are wonderful for commonly used objects. The System.Drawing.Color members are great examples as they represent a known-name colors that have obscure constructors (unless you know your hex colors). If they were implemented as enums you would have to do something like this every time you wanted to use the value as a color:

    colors c = colors.red;
    switch (c)
    {
        case colors.red:
            return System.Drawing.Color.FromArgb(255, 0, 0);
            break;
        case colors.green:
            return System.Drawing.Color.FromArgb(0,255,0);
            break;
    }
    

    So if you’ve got an enum and find that your constantly doing a switch/case/if/else/whatever to derive an object, you might want to use static class members. If you’re only querying the state of something, I’d stick with enums. Also, if you have to pass data around in an unsafe fashion enums will probably survive better than a serialized version of your object.

    Edit:
    @stakx, I think you stumbled on something important, too in response to @Anton’s post and that is complexity or more importantly, who is it complex for?

    From a consumer’s standpoint, I would immensely prefer System.Drawing.Color static class members over having to write all of that. From a producer’s standpoint, however, it would be a pain to have to write all of that. So if other people are going to be using your code you might be saving them a lot of trouble by using static class members even though it might take you 10 times as long to write/test/debug. However, if its just you you might find it easier to just use enums and cast/convert as needed.

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

Sidebar

Related Questions

My problem is that my company's website has recently been getting the following parse
Recently I've switched to Ninject 2.0 release and started getting the following error: Error
We have a .NET application that was recently converted to .NET 4.0 and then
I am getting following error. Restore failed for Server I have recently upgraded SQL
Recently i've switched to PHP 5.3+ and after that migration i learned that the
An interesting problem occured recently, and I've been thinking of the best way (for
Recently I found about this tool easy_install that help me to easy install additional
Recently I found some simple source code of a bootloader.The following is the simple
Recently I noticed (after others have extended the project) that the compile time significantly
I have been working with relational databases for sometime, but it only recently occurred

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.