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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:04:49+00:00 2026-05-31T09:04:49+00:00

I cannot understand why the code below compiles. public void Overflow() { Int16 s

  • 0

I cannot understand why the code below compiles.

public void Overflow()
{
    Int16 s = 32767;
    s = (Int16)  (s + 1);
}

At the compile time it is obvious that (s+1) is not a Int16 anymore as we know the value of s.

And CLR allows casting to :

  • To its own type
  • Or any of the base-types (because it is safe)

As Int32 is not Int16 and Int16 is not base type of Int32.

Question: So why the compiler does not fail for the casting above? Can you please explaint it from the CLR and compiler point of view?

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-05-31T09:04:50+00:00Added an answer on May 31, 2026 at 9:04 am

    The type of the expression s + 1 is Int32 – both operands are converted to Int32 before the addition is performed. So your code is equivalent to:

    public void Overflow()
    {
        Int16 s = 32767;
        s = (Int16)  ((Int32) s + (Int32) 1);
    }
    

    So the overflow only actually occurs in the explicit cast.

    Or, to put it another way: because the language specification says so. You should describe one of:

    • Why you think the compiler is violating the language specification
    • The exact change you’re proposing to the language specification

    EDIT: Just to make things really clear (based on your comments), the compiler wouldn’t allow this:

    s = s + 1;
    

    when s is an Int16 whatever the value of s might be known to be. There’s no Int16 operator+ (Int16, Int16) operator – as shown in section 7.8.4 of the C# 4 spec, the integer addition operators are:

    int operator +(int x, int y);
    uint operator +(uint x, uint y);
    long operator +(long x, long y);
    ulong operator +(ulong x, ulong y);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Am unable to understand why Cannot the below javascript code is not called from
I cannot understand why the block of code below is not passing even though
I cannot understand why the code below is giving me this error in firebug
I don't understand why the below code does not build: bool AguiRectangle::pointInside(const AguiPoint &p
foo\r\nbar.replace(/(foo).+/m, bar) Hello. I can not understand why this code does not replace foo
The below code compiles with gcc v4.3.3 and the templated child class seems to
I have the below code that links and runs fine in 32bit mode -
I understand that val keyword determines the underlying variable is a Immutable type (Cannot
I am trying to compile this piece of code found below. However, I got
Please help me to understand the following issue. Look at the code example below:

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.