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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:57:56+00:00 2026-06-04T19:57:56+00:00

I have an enum (of underlying type int ) with two values, and a

  • 0

I have an enum (of underlying type int) with two values, and a method that takes a parameter of that type. Is it possible to cast any int value to my enum type and pass it to the method? If so, what is the advantage of an enum? Isn’t it supposed to restrict the choice of values available?

class Program
{
    public void Greeting(MyCode code)
    {
        Console.WriteLine(code);
    }

    static void Main(string[] args)
    {
        var p = new Program();
        var m = new MyCode();

        m = (MyCode) 3;
        p.Greeting(m);
    }
}

public enum MyCode:int
{
   Hello =1,
   Hai
}
  • 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-04T19:57:58+00:00Added an answer on June 4, 2026 at 7:57 pm

    Yes, you can cast any value of the underlying type. The enum type is not a restrictive type in that sense.

    The advantages of using enums are:

    • Readability
    • Correctness when you don’t explicitly cast
    • Ability to access values via reflection etc

    I agree that a more type would be useful too, but it doesn’t exist in C#. You can build your own class-based “pseudo-enums” in C# which allow for a restricted set of values (using a private constructor), but:

    • You can’t make them non-nullable
    • You can’t switch on them
    • You can’t use them for constant values (such as optional parameter defaults)
    • The value will always be a reference, which can impact on memory usage (compared with an enum with an underlying type of byte, for example)
    • Preserving the restricted values in the face of serialization is interesting

    On the other hand, such types can act polymorphically and have more information than plain enums, which can be useful.

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

Sidebar

Related Questions

I have an enum that maps to possible field values in a char(1) field
If I have a strongly-typed enum, with say, underlying type int , is it
I have an enum, with two values. However, the second value contains a slash
I want to know why we can't have char as underlying enum type. As
I have an enum that I would like to randomly select a value from,
I have an enum type with a last item 'num_of_types'. (that is, it will
Is it possible to define operators for enums? For example I have enum Month
I have the enum structure as follows: public enum MyEnum { One=1, Two=2, Three=3
I have an enum type like this as an example: public Enum MyEnum {
I have a Enum defined as Type public Enum **Type** { OneType, TwoType, ThreeType

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.