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

The Archive Base Latest Questions

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

Consider the following code… double total = Int32.MaxValue; total++; int previousX = 0; for

  • 0

Consider the following code…

            double total = Int32.MaxValue;
            total++;

            int previousX = 0;

            for (var x = 0; x <= total; x++)
            {
                if (x == Int32.MaxValue)
                {
                    Console.WriteLine("Int32 max reached.");
                }

                if (x < 0)
                {
                    Console.WriteLine("Counter now < 0.");
                }

                previousX = x;
            }

It would appear that, if you use var with a for loop the default type inference is of an int.

Is this correct because if the counter goes past the maximum for an int 32, instead of overflowing the stack it resets itself back to zero and then counts down from zero.

Note: previousX allows you to set breakpoints and see what the previous value of the counter “x” was.

Does anyone know why this happens?

It seems possible to get into a bit of a pickle by using var for the counter of a for loop.

  • 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-26T15:38:24+00:00Added an answer on May 26, 2026 at 3:38 pm

    It is important to understand that the var keyword does not mean “variant” and does not indicate that the variable is loosely typed, or late-bound. It just means that the compiler determines and assigns the most appropriate type. As you have var x=0 it determines that x is an int in order to match 0.
    So yes it is correct functionality.

    Because of the manner in which negative numbers are represented (twos compliment) as a number wraps over its maximum value it will appear to reset and count down from -1 onwards.

    The increment on x++ doesn’t occur until that iteration of the loop has completed.

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

Sidebar

Related Questions

Consider the following code: abstract class SomeClassX<T> { // blah } class SomeClassY: SomeClassX<int>
Consider the following code: template <int dim> struct vec { vec normalize(); }; template
Consider the following code: procedure TForm1.PaintBox1Paint(Sender: TObject); var vRect : TRect; vFormat : TTextFormat;
Consider the following code: private static void WriteProcesses(StreamWriter sw, DateTime d) { sw.WriteLine(List of
Consider the following code // BOGP.cpp : Defines the entry point for the console
Consider the following code: struct Vec2 : IEquatable<Vec2> { double X,Y; public bool Equals(Vec2
Consider the following code snippet: std::vector<int> v; v.reserve(100); v.insert(v.end(), 100, 5); v.erase(v.begin(), v.end()); std::cout
Consider the following code: #include <stdio.h> int main (void) { char str1[128], str2[128], str3[128];
Consider the following code: class Rectangle { public: // Constructors Rectangle(){ init(0,0); } Rectangle(int
please consider following code #include <iostream> using namespace std; class Digit { private: int

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.