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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:39:24+00:00 2026-05-24T02:39:24+00:00

Why does addition require a cast but subtraction works without a cast? See the

  • 0

Why does addition require a cast but subtraction works without a cast? See the code below to understand what I am asking

public enum Stuff
{
    A = 1,
    B = 2,
    C = 3
}

var resultSub = Stuff.A - Stuff.B; // Compiles
var resultAdd = Stuff.A + Stuff.B; // Does not compile
var resultAdd2 = (int)Stuff.A + Stuff.B; // Compiles     

note: For both addition and subtraction it does not matter whether result is out of range (of the enum) or not in all three examples above.

  • 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-05-24T02:39:26+00:00Added an answer on May 24, 2026 at 2:39 am

    Good question – I was surprised that the first and third lines worked.

    However, they are supported in the C# language specification – in section 7.8.4, it talks about enumeration addition:

    Every enumeration type implicitly provides the following pre-defined operators, where E is the enum type and U is the underlying type of E:

    E operator +(E x, U y)
    E operator +(U x, E y)
    

    At runtime, these operators are ealuated exactly as (E)((U)x + (U)y)

    And in section 7.8.5:

    Every enumeration type implicitly provides the following predefined operator, where E is the enum type and U is the underlying type of E:

    U operator -(E x, E y)
    

    This operator is evaluated exactly as (U)((U)x - (U)y)). In other words, the operator computes the difference between the ordinal values of x and y, and the type of the result is the underlying type of the enumeration.

    E operator -(E x, U y);
    

    This operator is evaluated exactly as (E)((U)x - y). In other words, the operator subtracts a value from the underlying type of the enumeration, yielding a value of the enumeration.

    So that’s why the compiler behaves like that – because it’s what the C# spec says to do 🙂

    I wasn’t aware that any of these operators exist, and I’ve never knowingly seen them used. I suspect the reasons for their existence are buried somewhere in the language design meeting notes that Eric Lippert occasionally dives into – but I also wouldn’t be surprised if they were regretted as adding features for little benefit. Then again, maybe they’re really useful in some situations 🙂

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

Sidebar

Related Questions

I am going to write an expression evaluator which only does addition and subtraction.
What does Java do with long variables while performing addition? Wrong version 1: Vector
Does anyone know how to transform a enum value to a human readable value?
I'm completely ignorant of SQL/databases, but I was chatting with a friend who does
Does Google force employees who have offers from Facebook to leave immediately?
Does anyone remember the XMP tag? What was it used for and why was
Does the Java language have delegate features, similar to how C# has support for
Does anybody know any good resources for learning how to program CIL with in-depth
Does anyone know how to get IntelliSense to work reliably when working in C/C++
Does anyone have any recommendations of tools that can be of assistance with moving

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.