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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:59:47+00:00 2026-06-10T01:59:47+00:00

I currently have an enum defined as follows: public enum ColorEnum { Red, White,

  • 0

I currently have an enum defined as follows:

public enum ColorEnum { Red, White, Blue, Green };

Each of these enum values has an underlying int value that I need to be able to return to the user. To do so, I wrote a function that takes in the enum and returns an int:

public int colorToValue(ColorEnum color)
{
    switch (color)
    {
        case Red:
            return 5;
        case White:
            return 10;
        case Blue:
            return 15;
        case Green:
            return 20;
    }

    return 0;
}

This works well and good but I was wondering if I could simplify things by using the enum itself to store the underlying values. For example, could I just define the enum as follows:

public enum ColorEnum { Red = 5, White = 10, Blue = 15, Green = 20 };

Then converting the enum to it’s underlying value would be as simple as casting it to an int. The above function simplifies to the following:

public int colorToValue(ColorEnum color)
{
    return (int)color;
}

Of course, the values for each enum entry would have to be an int as required by the enum definition.

Thanks!

  • 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-06-10T01:59:48+00:00Added an answer on June 10, 2026 at 1:59 am

    Yes that is entirely valid and will work fine. Enums cast cleanly to/from their underlying type, and it is very common for them to have prescribed values rather than just 1,2,3…

    The only thing to watch, though, is that the reverse is true too. So this works fine:

    ColorEnum evil = (ColorEnum)(-53); // why not...
    

    Your original colorToValue(evil) code would return 0 for this; your new code will return -53. Personally I might have thrown an ArgumentOutOfRangeException.

    There is also an Enum.IsDefined method, but that involves boxing the value. But if you do want to check the input is an expected value, at least you can simplify:

    public int colorToValue(ColorEnum color)
    {
        switch (color)
        {
            case Red:
            case White:
            case Blue:
            case Green:
                return (int)color;
            default:
                return 0; // or throw, whatever
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I Have an Enum Called ActionStatus that has 2 possible values open=0 and closed
I currently use an enum defined as such : public enum Language{ English(english, eng,
I have an enum: public enum baseKey : uint { HKEY_CLASSES_ROOT = 0x80000000, HKEY_CURRENT_USER
I currently have one project that currently contains multiple packages. These packages make up
I currently have 4 tables. Product, Category, SubCategory, ProductSubCategory. Category has a OneToMany SubCategory
My question is best illustrated with an example. Suppose I have the enum: public
I have this enum: [Flags] public enum ExportFormat { None = 0, Csv =
There has to be a cleaner method. Currently I have: ... Constructor() { parseDictionary
I have several Enum types defined across my project, used for object-specific state identifiers:
I have the follwoing enum in my backend java code: public static enum CountryCodes

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.