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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:30:38+00:00 2026-06-14T12:30:38+00:00

The MSDN examples are just compile time errors. It’s just I see @JonSkeet uses

  • 0

The MSDN examples are just compile time errors.

It’s just I see @JonSkeet uses it an answer here: https://stackoverflow.com/a/263416/360211 and I can’t see why if it’s just compile time.

    static void Main()
    {
        const int x = 4;
        int y = int.MaxValue;
        int z = x*y;
        Console.WriteLine(z);
        Console.ReadLine();
    }

Produces -4, same as this:

    static void Main()
    {
        unchecked
        {
            const int x = 4;
            const int y = int.MaxValue;
            int z = x*y; // without unchecked, this is compile error
            Console.WriteLine(z);
            Console.ReadLine();
        }
    }

This throws runtime:

    static void Main()
    {
        checked
        {
            const int x = 4;
            int y = int.MaxValue;
            int z = x*y; //run time error, can checked be set system wide?
            Console.WriteLine(z);
            Console.ReadLine();
        }
    }

So is Jon doing this because it could be set system wide, compiler flag or otherwise?

  • 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-06-14T12:30:39+00:00Added an answer on June 14, 2026 at 12:30 pm

    Whether an operation is in a checked or unchecked block is executed when the values are actually computed. When multiplying/adding two variables this will almost always be at runtime, since they aren’t known at compile time.

    In your example the two variables are both constants; this means that the result of the computation between compile time literals can be done at compile time (which it is). Since the result is being computed at compile time, and it’s not in an unchecked block, it can’t compute the result without an error. That does not mean that checked/unchecked blocks are generally a compile time check; they very rarely are.

    And yes, it is possible to change the project properties for whether the entire code base is in a checked/unchecked context. If you’re writing a library it’s also possible for the method to be called from within either context. For that reason, if there’s a block of code that must run in either a checked or unchecked context it’s a good practice to add such a block, even if your current project setting is correct for that method. Also, if nothing else, it’s making the intent of that snippet clearer to future readers, “This method relies on integer overflow,” vs., “This method must never overflow.”

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

Sidebar

Related Questions

I am just trying to compile this simple example : http://msdn.microsoft.com/en-us/library/ms533895(VS.85).aspx what needs to
I read the MSDN documentation and examples here and I know that the correct
I've read a lot of examples/tutorials (incl. Ayende's Alexandria on MSDN). But just getting
I'm struggling with compile error C3200 http://msdn.microsoft.com/en-us/library/3xwxftta.aspx discusses the problem and gives as an
Easy-to-follow examples of CRD (Create, Read, Delete) appear in MSDN here There is a
I'm working through some MSDN examples, and some books on ADO.Net. What they all
I used this MSDN example to construct my console host app: http://msdn.microsoft.com/en-us/library/ms731758.aspx It works
I looked at this example http://msdn.microsoft.com/en-us/library/bb399420.aspx and clicked on DataContext.CreateDatabase which brought me to
I'm using the SSLStream example from msdn here . The client code seems to
I'm looking at the TPL exception handling example from MSDN @ http://msdn.microsoft.com/en-us/library/dd537614(v=VS.100).aspx The basic

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.