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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:30:00+00:00 2026-05-31T21:30:00+00:00

I have some code static void Main(string[] args) { int j = 0; for

  • 0

I have some code

static void Main(string[] args)
{
    int j = 0;
    for (int i = 0; i < 10; i++) 
        j = j++;
    Console.WriteLine(j);
}

Why answer is 0 ?

  • 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-31T21:30:02+00:00Added an answer on May 31, 2026 at 9:30 pm

    This is because of the way the ++ increment works. The order of operations is explained in this MSDN article This can be seen here (somebody please correct me if I am reading the spec on this one wrong :)):

    int j = 2;
    //Since these are value objects, these are two totally different objects now
    int intermediateValue = j;
    j = 2 + 1
    //j is 3 at this point
    j = intermediateValue;
    //However j = 2 in the end
    

    Since it is a value object, the two objects (j and intermediateValue) at that point are different. The old j was incremented, but because you used the same variable name, it is lost to you. I would suggest reading up on the difference of value objects versus reference objects, also.

    If you had used a separate name for the variable, then you would be able to see this breakdown better.

    int j = 0;
    int y = j++;
    Console.WriteLine(j);
    Console.WriteLine(y);
    //Output is 
    // 1
    // 0
    

    If this were a reference object with a similar operator, then this would most likely work as expected. Especially pointing out how only a new pointer to the same reference is created.

    public class ReferenceTest
    {
        public int j;
    }
    
    ReferenceTest test = new ReferenceTest();
    test.j = 0;
    ReferenceTest test2 = test;
    //test2 and test both point to the same memory location
    //thus everything within them is really one and the same
    test2.j++;
    Console.WriteLine(test.j);
    //Output: 1
    

    Back to the original point, though 🙂

    If you do the following, then you will get the expected result.

    j = ++j;
    

    This is because the increment occurs first, then the assignment.

    However, ++ can be used on its own. So, I would just rewrite this as

    j++;
    

    As it simply translates into

    j = j + 1;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: class Program { static void Main(string[] args) { Console.WriteLine(SqrtRoot(0));
I have some code that looks like: static const std::string and( AND ); This
class Program { static void Main(string[] args) { String value = Two; Type enumType
I have some code where I use a thread static object in C#. [ThreadStatic]
I have some code shared among multiple projects in a static library. Even with
I have some simple code in which the main Thread is creating a new
In an application I have some code that has a FieldInfo for a Nullable<int>
I have the following console application: private static bool run = false; static void
I have some code which populates a hashtable with a question as the key
I have some code using a System.Transactions.TransactionScope , that creating a new instance of

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.