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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:02:33+00:00 2026-05-25T03:02:33+00:00

Just noticed that the unchecked context doesn’t work when working with a BigInteger, for

  • 0

Just noticed that the unchecked context doesn’t work when working with a BigInteger, for instance:

unchecked
{
    // no exception, long1 assigned to -1 as expected
    var long1 = (long)ulong.Parse(ulong.MaxValue.ToString());
}

unchecked
{
    var bigInt = BigInteger.Parse(ulong.MaxValue.ToString());

    // throws overflow exception
    var long2 = (long)bigInt;
}

Any idea why that’s the case? Is there something special with the way big integers are converted to other primitive integer types?

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-25T03:02:35+00:00Added an answer on May 25, 2026 at 3:02 am

    The C# compiler has no idea whatsoever that a BigInteger is logically an “integral type”. It just sees a user-defined type with a user-defined explicit conversion to long. From the compiler’s point of view,

    long long2 = (long)bigInt;
    

    is exactly the same as:

    long long2 = someObject.SomeMethodWithAFunnyNameThatReturnsALong();
    

    It has no ability to reach inside that method and tell it to stop throwing exceptions.

    But when the compiler sees

    int x = (int) someLong;
    

    the compiler is generating the code doing the conversion, so it can choose to generate checked or unchecked code as it sees fit.

    Remember, “checked” and “unchecked” have no effect at runtime; it’s not like the CLR goes into “unchecked mode” when control enters an unchecked context. “checked” and “unchecked” are instructions to the compiler about what sort of code to generate inside the block. They only have an effect at compile time, and the compilation of the BigInt conversion to long has already happened. Its behaviour is fixed.

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

Sidebar

Related Questions

I just noticed that my old codes written in python 2.5 does not work
I've just noticed that Array doesn't override the triple equal sign method === ,
I have a great working 3 panel liquid layout but I just noticed that
I just noticed that the .NET Framework doesn't include Cuba in the CultureInfo class
I just noticed that EditorGridPanel doesn't have row over effect as in GridPanel so
I have just noticed that when I have a running instance of my GAE
I just noticed that adding context to the selector is much faster than refining
Just noticed that the style of the navigation menu on windows.com is just what
I just noticed that the return list for results is limited to 1000. I
I just noticed that Chromium was installed in AppData in both Vista and XP.

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.