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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T08:46:27+00:00 2026-05-21T08:46:27+00:00

byte b; int i; unchecked { b = 255 + 255; //overflows i =

  • 0
byte b;
int i;

unchecked
{
    b = 255 + 255; //overflows
    i = 100 + int.MaxValue+100; // works

}

1) Is the reason why b expression (b = 255 + 255;) causes an overflow error due to being affected by two conflicting rules, where first rule R1 states:

A constant-expression (§7.19) of type
int can be converted to type sbyte,
byte, short, ushort, uint, or ulong,
provided the value of the
constant-expression is within the
range of the destination type.

while second rule R2 states that within unchecked context overflow is allowed.

And in the case of b expression, R1 takes precedence over R2, and thus since constant expression 255+ 255 is not within the range of destination type ( which is byte ), R1 causes an error, even though R2 permits an overflow?

2)

a) Here’s my reasoning as to why i expression (i = 100 + int.MaxValue+100;) doesn’t cause an error:

1 – When compiler starts computing i expression, it doesn’t try to promote values 100 and int.MaxValue to type long before performing the addition ( thus during the computation process the two values are still of type int )

2 – the addition does cause an overflow, but since this happens within unchecked context, no error is thrown

3 – Since the two values didn’t get promoted to long, the resulting value is also of type int and as such the resulting value is within the range of destination type

b) But if instead compiler did promote 100 and int.MaxValue; to type long before performing the addition, then i expression would cause an error due to violation of rule R1?!

thanx

  • 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-21T08:46:28+00:00Added an answer on May 21, 2026 at 8:46 am

    Yes. The constants are all int by default, so the second statement will overflow happily and stay within int. You can see that it fails if you make one of the constants long:

    byte b;
    int i;
    
    unchecked
    {
        b = 255 + 255; //overflows
        i = 100L + int.MaxValue+100; // fails as well
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

int int1; byte byte1; unchecked { int1 = 2147483647 + 10; //ok byte1 =
Why the allocation of a byte array with the size int.MaxValue fails byte[] array1
How to Convert a byte array into an int array? I have a byte
int val = 233; byte b = (byte) val; System.out.println(b); I have a simple
protected IplImage processImage(byte[] data, int width, int height) { int f = 4;// SUBSAMPLING_FACTOR;
I want to convert an int to a byte[2] array using BCD. The int
(background: Why should I use int instead of a byte or short in C#
for( int i = 0; i < lines; i++ ) { std::unique_ptr<BYTE[]> pLine( new
I have the following function: public static byte[] StringToByte(string str) { int length =
int start=0,flag=1; long size=blobInfo.getSize(),fetched=0,fetch; byte temp[] = null; while(fetched<size){ if(size-fetched>MAX_BLOB_FETCH_SIZE) fetch=MAX_BLOB_FETCH_SIZE; else fetch=size-fetched; temp=blobstoreService.fetchData(blobKey,fetched,fetch

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.