# simple arythmetic question C#

• ### Question

• int a = 3;
int b = 5;
int c = 40;
<b>int d = c-- - b * a;</b>
Console.WriteLine(\$"a={a}, b={b} c={c} d={d}");

an order of "int d" hadnling is like this :

39    15
int d = (c--)-(b*a); == 24? of cource not! <b>d=25</b>!

because in fact an exceution order is following:

<b>int d = c-(b*a)</b>; and c-- is postdecrement operation and lost somehow

and the c# editor divides the string like this

so thiese expressons are equal

int d = c-(b*a)           and
int d = (c--)-(b*a)

<b>Could somebody explain how?</b>

and moreover!

if you'll try to use QuickWatch on (c-- - b * a), it fulfilles the procedure in the background and <b>changes the c</b> so when you try to use it again, the result will be 24! because the c has being changed.
<b>I believe it is not correct</b>
because QuickWatch should execute the expression, but in the different place somewhere and shouldn't change the variable.

• Moved by Tuesday, April 2, 2019 1:57 PM VS related
Tuesday, April 2, 2019 7:56 AM

### All replies

• Hi

Thank you for posting here.

>>int d = c-(b*a)   and int d = (c- -)-(b*a)

For your question, you want to know why the above expressions are equal.

The meaning of ‘c- -‘ is to decrement c after the execution of this expression is completed.

You could refer to the following gif to understand it.

If you want to decrement c, you could try the following code.

`     int d = - -c - b * a;`

Best regards,

Jack

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

Tuesday, April 2, 2019 9:22 AM
• Hi!

Thank you!

I've discussed this on the CodeProject already:

https://www.codeproject.com/Questions/1351070/Simple-arythmetic-question-Csharp

The question was not about the syntax or understanding of an operation execution.

This is abount evidence of an execution.

Mostly the question to the support team regarding to the following:

if you'll try to use QuickWatch on (c-- - b * a), it fulfilles the procedure in the background and changes the c variable so when you try to use it again, the result will be 24! because the c has being changed.
I believe it is not correct
because QuickWatch should execute the expression, but in the different place somewhere and shouldn't change the variable.

Tuesday, April 2, 2019 9:50 AM
• That isn't how quick watch or the debugger works. In fact it cannot work that way. The debugger has to inject itself into your debugging process in order to show you the data you need. Because your data may rely on static fields or other data the debugger has no way of running any of your code anywhere other than in the context of the app itself.

Running code inside the debugger always (irrelevant of OS, language and IDE) has the potential to impact the running code. There is really no (reasonable) way to do otherwise. Short of snapshotting your process and running it separately (which can then break other areas of your app such as connections to databases or threading) there is no way for the compiler to know that evaluating some arbitrary expression is going to have a side effect. There isn't a debugger anywhere that I'm aware of that would try to do such a thing.

As a developer you are the only person who knows whether your code will have side effects or not. It is your responsibility to understand the ramifications of executing code and, if necessary, adjusting your debugging to account for it.

Michael Taylor http://www.michaeltaylorp3.net

Tuesday, April 2, 2019 1:57 PM
• hm...

in fact these are not of my "side effects" - I mean the debugger procedure QuickWatch.

You've evaluated an expression. I can see the result.

Then why shouldn't you just drop the result without saving it back to the lvalue?

Tuesday, April 2, 2019 2:29 PM
• "in fact these are not of my "side effects""

The `c--` operator has the side effect. If you tell the debugger to eval that expression then the side effect kicks in. It is no different than if you asked the debugger to execute a function which had a side effect. The debugger is going to execute your code just like your app would, that is the whole point. Side effects are part of the reason the compiler cannot rewrite some expressions, the compiler has to guarantee that any optimizations do not change the side effects of evaluating expressions. As I mentioned before, debuggers have always worked this way because it would be prohibitive (if not impossible) to do otherwise and still guarantee that the debugger executes the code in the same manner as how it would actually run. Side effects of expressions are up to the developer to handle.

But your original problem you posted about, which I assume sent you down the debugger route, is that your code wasn't producing the expected output. When I put your code into the debugger and run it the console prints out d = 25 like I expected. Did you not get this result? Of course if you selected the `c--` expression and put a watch on it (or used quickwatch and then reevaluated it) then it would run the expression which triggers the side effect. Each time the watch window updates (or you reeval in quickwatch) it would run again and the side effect would take effect. This is expected behavior and consistent with how the debugger runs all expressions (see function example of earlier). If you wanted to "reset" the value after viewing it in the debugger then you'd simply reset the `c` variable to its initial value. But this is on you, the developer, to fix the value since you tampered with it by using it in a side effect expression in the debugger.

Michael Taylor http://www.michaeltaylorp3.net

Tuesday, April 2, 2019 5:31 PM