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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:45:16+00:00 2026-06-01T15:45:16+00:00

I found a goodlooking example about implementation enums in a different way. That is

  • 0

I found a goodlooking example about implementation enums in a different way. That is called type-safe enum pattern i think. I started using it but i realized that i can not use it in a switch statement.
My implementation looks like the following:

public sealed class MyState
{
    private readonly string m_Name;
    private readonly int m_Value;

    public static readonly MyState PASSED= new MyState(1, "OK");
    public static readonly MyState FAILED= new MyState(2, "ERROR");

    private MyState(int value, string name)
    {
        m_Name = name;
        m_Value = value;
    }

    public override string ToString()
    {
        return m_Name;
    }

    public int GetIntValue()
    {
        return m_Value;
    }
}

What can i add to my class in order to be able to use this pattern in switch statements in C#?
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-01T15:45:18+00:00Added an answer on June 1, 2026 at 3:45 pm

    You can try something like this:

    class Program
    {
        static void Main(string[] args)
        {
            Gender gender = Gender.Unknown;
    
            switch (gender)
            {
                case Gender.Enum.Male:
                    break;
                case Gender.Enum.Female:
                    break;
                case Gender.Enum.Unknown:
                    break;
            }
        }
    }
    
    public class Gender : NameValue
    {
        private Gender(int value, string name)
            : base(value, name)
        {
        }
    
        public static readonly Gender Unknown = new Gender(Enum.Unknown, "Unknown");
        public static readonly Gender Male = new Gender(Enum.Male, "Male");
        public static readonly Gender Female = new Gender(Enum.Female, "Female");
        public class Enum
        {
            public const int Unknown = -1;
            public const int Male = 1;
            public const int Female = 2;
        }
    
    }
    
    public abstract class NameValue
    {
        private readonly int _value;
        private readonly string _name;
    
        protected NameValue(int value, string name)
        {
            _value = value;
            _name = name;
        }
    
        public int Value
        {
            get { return _value; }
        }
    
        public string Name
        {
            get { return _name; }
        }
    
        public override string ToString()
        {
            return Name;
        }
        public override int GetHashCode()
        {
            return Value.GetHashCode();
        }
    
        public override bool Equals(object obj)
        {
            NameValue other = obj as NameValue;
            if (ReferenceEquals(other, null)) return false;
            return this.Value == other.Value;
        }
    
        public static implicit operator int(NameValue nameValue)
        {
            return nameValue.Value;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Found this question that explained a way to communicate between layers. However there is
Found a bunch of other threads about comboboxes in WPF datagrid, but none that
Found out about this cool jQuery countdown which is initialized as such: <script type="text/javascript">
Found a piece of code today, that I find a little smelly... TMyObject.LoadFromFile(const filename:
Found this is some example CSS I was reading and I'm having a hard
I found that using Smarty with PHP, sometimes extra time will need to be
Found more than 10 tutorials to cursors but I can't understand that. My question
Found the following in the example code appserver.cpp distributed with UDT hints.ai_flags = AI_PASSIVE;
found that code to display html format text to dynamic textfield in as3: var
Found the following in an Oracle-based application that we're migrating (generalized) : SELECT Table1.Category1,

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.