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

  • Home
  • SEARCH
  • 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 6703897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:13:33+00:00 2026-05-26T07:13:33+00:00

Is there anyone able to explain me this strange behavior? int i = 0x1234;

  • 0

Is there anyone able to explain me this strange behavior?

    int i = 0x1234;
    byte b1 = (byte)i;
    byte b2 = (byte)0x1234;         //error: const value '4660' can't convert to byte (use unchecked)
    byte b3 = unchecked((byte)0x1234);
    byte b4 = checked((byte)i);     //throws
    byte b5 = (byte)(int)0x1234;    //error: same as above

NOTE: It’s an empty Console application, with NO arithmetic checking enabled (as default is).
Thank you everybody in advance.

EDIT: I supposed to be clear enough, but not for all.

I do know that a word can’t fit into a byte. But, by default, a C# program allows certain “dangerous” operations, primarily for performance reason.

Similarly, I may sum two large integers together and having no overflow at all.

My wonder was about the compile-time error above: the b1 cast/assignment is compiled, the b2 can’t compile. Apparently there’s no difference, because both are Int32 having the same value.

Hope it’s clear now.

  • 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-26T07:13:33+00:00Added an answer on May 26, 2026 at 7:13 am

    You’re tripping over a part of section 7.19 of the C# 4 spec:

    Unless a constant expression is explicitly placed in an unchecked context, overflows that occur in integral-type arithmetic operations and conversions during the compile-time evaluation of the expression always cause compile-time errors.

    Basically, the point is that even if you’re happy to allow operations to overflow at execution time, if you’re trying to use a constant expression which can’t be converted to the target type at compile time, you have to tell the compiler that you really know what you’re doing.

    For example, in this case you’re losing information – it’ll be equivalent to

    byte b3 = 0x34;
    

    so you’d normally be better off just specifying that, to give you clearer code which doesn’t mislead the reader. It’s relatively rare that you want to overflow in a constant – most of the time you should just specify the valid value instead.

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

Sidebar

Related Questions

Is there anyone who have encountered Processing Dirty Regions error in MyEclipse? Actually everytime
Is there anyone who knows this? I have been trying this for the last
I was wondering if anyone might be able to explain to me why it's
http://www.liveprofile.com/ I was wondering if anyone would be able to explain roughly how an
I can't seem to find an answer out there for this. Here's the scenario:
Guys, can anyone explain the following scenario: 1) Web application has module1.jar in its
Is there anyone working solo and using fogbugz out there? I'm interested in personal
Are there anyone with experiences/examples on releasing early/releasing often for commercial software? Does it
is there anyone who makes use of the Microsoft Open XML SDK 2.0 for
Is there anyone who has used or looked into using Jitterbit as well as

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.