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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:50:20+00:00 2026-06-14T11:50:20+00:00

A struct with constants: public struct UserType { public const int Admin=1; public const

  • 0

A struct with constants:

public struct UserType
{
  public const int Admin=1;
  public const int Browser=2;
  public const int Operator=3;
}

And now let’s use an enum for the same purpose:

public enum UserType
{
  Admin=1,
  Browser,
  Operator
}  

Both are allocated on the stack. In both cases I will say UserType.Admin. And with the struct way I will not have to cast to int to get the underlying value.I know that with the enum version it’s guaranteed that one and only one of the three values will be used, whereas with the struct version any integer can be used, which means any value between Int32.MinValue and Int32.MaxValue. Is there any other benefit of preferring enums besides this one?

  • 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-14T11:50:21+00:00Added an answer on June 14, 2026 at 11:50 am

    Is there any other benefit of preferring enums besides this one?

    Clarity.

    Suppose you have a field or a method parameter which will always have one of those three values. If you make it the enum type, then:

    • You can’t accidentally assign it some arbitrary integer value. (You can deliberately cast any int value to the enum type, but you’d have to do so explicitly.)
    • It’s clear what kind of information that value is meant to represent

    These are incredibly important benefits in writing code which is easy to maintain in the future. The more you can make your code naturally describe what you’re trying to achieve, the better.

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

Sidebar

Related Questions

struct Foo { int data; Foo() = default; Foo(const Foo& arg) = default; };
struct Div { int i; int j; }; class A { public: A(); Div&
struct Drink { public string Name { get; private set; } public int Popularity
Why can't I use sizeof() on simple structs? eg: private struct FloatShortPair { public
struct pointsto_val_def { unsigned int lhs; bitmap rhs; struct pointsto_val_def *next; }; typedef struct
struct { char a; int b; } x; Why would one define a struct
struct SomeStruct { int a; int b; }; SomeStruct someFn( int init ) {
struct struct0 { int a; }; struct struct1 { struct struct0 structure0; int b;
Assuming this: public class GenericMethods { public T Method<T>() where T : struct {
Let's examine the MSIL code generated for the following generic method: public static U

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.