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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:26:28+00:00 2026-05-24T22:26:28+00:00

If we are using the following loop in a program, the loop never ends

  • 0

If we are using the following loop in a program, the loop never ends in C# 4.0

for (int i = 1; i <= int.MaxValue; i++)
{
}

This is because adding 1 to int.MaxValue (2147483647) will not result in an overflow exception, but results in -2147483648 (taking into consideration 32bit int and 2’s compliment).

int i = int.MaxValue;
Console.WriteLine(i + 1);

It seems the behavior changed recently. See the question Arithmetic operation caused OverflowException .What could be the reason behind this change?

  • 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-24T22:26:30+00:00Added an answer on May 24, 2026 at 10:26 pm

    Overflow exceptions for integer (and other integral types) are only done in checked contexts.

    So, this will cause an exception:

    checked
    {
     int i = int.MaxValue;
      Console.WriteLine(i + 1);
    }
    

    They are not set to do this by default as they are more expensive than simply overflowing.

    From MSDN:

    The checked keyword is used to explicitly enable overflow checking for integral-type arithmetic operations and conversions.

    And:

    Overflow checking can be enabled by compiler options, environment configuration, or use of the checked keyword.


    This is not a recent change – C# has been like this from day one. What you see in the question is VB.NET code, which is by default in a checked context.

    So, keeping to defaults, overflowing code in VB.NET will throw an exception, but identical code in C# will not.

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

Sidebar

Related Questions

I'm using the following code to loop through a directory to print out the
I'm using the following syntax to loop through a list collection: For Each PropertyActor
I am using the following function to loop through a couple of open CDB
I have following foreach-loop: using System.IO; //... if (Directory.Exists(path)) { foreach(string strFile in Directory.GetFiles(path,
Consider the following code: namespace DisposeTest { using System; class Program { static void
Had to write the following program for an on line pre java class using,
I am using the following code: #include <stdio.h> #include <iostream> using namespace std; int
I have a Program in R where i'm using do while loop in the
In the following program: int main(){ std::cout<<enter numbers to be divide<<std::endl; int a,b,c; while(true){
In the following C code I am using OpenMP in a nested loop. Since

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.