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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:35:59+00:00 2026-05-23T14:35:59+00:00

Note: Please note that the code below is essentially non-sense, and just for illustration

  • 0

Note: Please note that the code below is essentially non-sense, and just for illustration purposes.

Based on the fact that the right-hand side of an assignment must always be evaluated before it’s value is assigned to the left-hand side variable, and that increment operations such as ++ and -- are always performed right after evaluation, I would not expect the following code to work:

string[] newArray1 = new[] {"1", "2", "3", "4"};
string[] newArray2 = new string[4];

int IndTmp = 0;

foreach (string TmpString in newArray1)
{
    newArray2[IndTmp] = newArray1[IndTmp++];
}

Rather, I would expect newArray1[0] to be assigned to newArray2[1], newArray1[1] to newArray[2] and so on up to the point of throwing a System.IndexOutOfBoundsException . Instead, and to my great surprise, the version that throws the exception is

string[] newArray1 = new[] {"1", "2", "3", "4"};
string[] newArray2 = new string[4];

int IndTmp = 0;

foreach (string TmpString in newArray1)
{
    newArray2[IndTmp++] = newArray1[IndTmp];
}

Since, in my understanding, the compiler first evaluates the RHS, assigns it to the LHS and only then increments this is to me an unexpected behaviour. Or is it really expected and I am clearly missing something?

  • 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-23T14:36:00+00:00Added an answer on May 23, 2026 at 2:36 pm

    It is instructive to see exactly where your error is:

    the right-hand side of an assignment must always be evaluated before it’s value is assigned to the left-hand side variable

    Correct. Clearly the side effect of the assignment cannot happen until after the value being assigned has been computed.

    increment operations such as ++ and — are always performed right after evaluation

    Almost correct. It is not clear what you mean by “evaluation” — evaluation of what? The original value, the incremented value, or the value of the expression? The easiest way to think about it is that the original value is computed, then the incremented value, then the side effect happens. Then the final value is that one of the original or the incremented value is chosen, depending on whether the operator was prefix or postfix. But your basic premise is pretty good: that the side effect of the increment happens immediately after the final value is determined, and then the final value is produced.

    You then seem to be concluding a falsehood from these two correct premises, namely, that the side effects of the left hand side are produced after the evaluation of the right hand side. But nothing in those two premises implies this conclusion! You’ve just pulled that conclusion out of thin air.

    It would be more clear if you stated a third correct premise:

    the storage location associated with the left-hand-side variable also must be known before the assignment takes place.

    Clearly this is true. You need to know two things before an assignment can happen: what value is being assigned, and what memory location is being mutated. You can’t figure those two things out at the same time; you have to figure out one of them first, and we figure out the one on the left hand side — the variable — first in C#. If figuring out where the storage is located causes a side effect then that side effect is produced before we figure out the second thing — the value being assigned to the variable.

    In short, in C# the order of evaluations in an assignment to a variable goes like this:

    • side effects of the left hand side happen and a variable is produced
    • side effects of the right hand side happen and a value is produced
    • the value is implicitly converted to the type of the left hand side, which may produce a third side effect
    • the side effect of the assignment — the mutation of the variable to have the value of the correct type — happens, and a value — the value just assigned to the left hand side — is produced.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please note that I am not a java guru. I might not use the
I have the code below, where I want to get the id of a
I have the following code below to generate an OpenSSL RSA public and private
I have an image that links to my homepage. Is there an attribute that
Could someone please explain why this is happening. I have simplified my problem by
I need a way of grabbing the most recent item added to a ReplaySubject
I'm learning as I go with c# and I have been stuck with some
I've got Collabnet SVN server installed on windows. Additionally, I have implemented a post-commit
I've created a cursor to select the actual data, and loop through it outputting

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.