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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:00:34+00:00 2026-06-17T22:00:34+00:00

The C# compiler allows operations between different enum types in another enum type declaration,

  • 0

The C# compiler allows operations between different enum types in another enum type declaration, like this:

public enum VerticalAnchors
{
    Top=1,
    Mid=2,
    Bot=4
}

public enum HorizontalAnchors
{
    Lef=8,
    Mid=16,
    Rig=32
}

public enum VisualAnchors
{
    TopLef = VerticalAnchors.Top | HorizontalAnchors.Lef,
    TopMid = VerticalAnchors.Top | HorizontalAnchors.Mid,
    TopRig = VerticalAnchors.Top | HorizontalAnchors.Rig,
    MidLef = VerticalAnchors.Mid | HorizontalAnchors.Lef,
    MidMid = VerticalAnchors.Mid | HorizontalAnchors.Mid,
    MidRig = VerticalAnchors.Mid | HorizontalAnchors.Rig,
    BotLef = VerticalAnchors.Bot | HorizontalAnchors.Lef,
    BotMid = VerticalAnchors.Bot | HorizontalAnchors.Mid,
    BotRig = VerticalAnchors.Bot | HorizontalAnchors.Rig
}

but forbids them inside method code, i.e. the operation:

VerticalAnchors.Top | HorizontalAnchors.Lef;

Is flagged with this error:

Operator ‘|’ cannot be applied to operands of type ‘VerticalAnchors’ and ‘HorizontalAnchors’.

There’s a workaround, of course:

(int)VerticalAnchors.Top | (int)HorizontalAnchors.Lef

I am curious about this compiler behaviour. Why are operations between different enum types allowed in another enum declaration but not elsewhere?

  • 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-17T22:00:35+00:00Added an answer on June 17, 2026 at 10:00 pm

    It is actually not in the spec as far as I can tell. There is something related:

    If the declaration of the enum member has a constant-expression
    initializer, the value of that constant expression, implicitly
    converted to the underlying type of the enum, is the associated value
    of the enum member.

    Although VerticalAnchors.Top & HorizontalAnchors.Lef has type VerticalAnchors it can be implicitly converted to VisualAnchors. But this does not explain why the constant expression itself supports implicit conversions everywhere.

    Actually, it appears explicitly to be against the spec:

    The compile-time evaluation of constant expressions uses the same
    rules as run-time evaluation
    of non-constant expressions, except that
    where run-time evaluation would have thrown an exception, compile-time
    evaluation causes a compile-time error to occur.

    If I didn’t miss something, the spec not only does not allows this explicitly, it disallows it. Under that assumption it would be a compiler bug.

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

Sidebar

Related Questions

I'd like to build a C pre-processor / compiler that allows functions to be
The compiler complains about this, after I activated all kind of warnings: For MyApp_Prefix.pch
The compiler (VC8) error is: error C2680: 'std::_Tree<_Traits>::iterator' : invalid target type for dynamic_cast
I have a number of operands of different types and want to define how
I'm trying to implement a function which allows the user to input some type
I'm having trouble understanding why the java-compiler allows non-specific collections to be assigned collections
This is an offshoot of these two questions: 1 , 2 . I'd like
I want to dynamically add .Select statements, but the compiler will only allow me
Using the commandline visual basic compiler (vbc.exe) with option VBruntime- does not allow using
My compiler expands it to 199711L. What does that mean? I read that __cplusplus

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.