What's happenig behind the scene so the code returns 1 and not 43?

What's happenig behind the scene so the code returns 1 and not 43?



Found this in one book, but the explanation was too short.


public class Program

int a = 0;

private static void Main()

var val = new Program();
val.a += val.Foo();

Console.WriteLine(val.a);
Console.ReadKey();

private int Foo()

a = a + 42;
return 1;




Does it have to do something with boxing or not?





return 1 is probably not what you wanted.
– jmoerdyk
Aug 24 at 19:10


return 1





It's a quiz from a book.
– Timofeus
Aug 25 at 18:12




3 Answers
3



This doesn't have anything to do with boxing, it is order of operations...


class Program

int a = 0;

static void Main()

Program val = new Program();
val.a += val.Foo();
Console.WriteLine(val.a);

Console.ReadKey();


int Foo()

a = a + 42;
return 1;




So what happens is


val.a += val.Foo();



Is essentially re-written as


val.a = val.a + val.Foo();



Because of order of operations, here is what gets pushed on the stack:



So when the evaluation of val.a += val.Foo() begins, it saves the current value of val.a, which is zero, then calls the function. The function modifies val.a, but because it is a value type, it does not update the saved value in the original caller. Once val.Foo() returns, the equation then becomes val.a = 0 + 1, hence the result is 1 and not 43.


val.a += val.Foo()


val.a


val.a


val.Foo()


val.a = 0 + 1


1


43



If it were re-written slightly, you would get a different result:


val.a = val.Foo() + val.a;



Would then result in 43. This is an order-of-operations problem.



Let's have a look:


// val.a = 0
Program val = new Program();
// val.a += val.Foo() can be rewritten as
// val.a = val.a + val.Foo() or initial value of val.a + result of val.Foo()
// val.a = 0 + 1
val.a += val.Foo();
// print out 1
Console.WriteLine(val.a);



Edit: If you want to exploit side effect (assigning a to 42 within Foo() method), you can put (instead of val.a = val.a + val.Foo()):


a


42


Foo()


val.a = val.a + val.Foo()


// result of val.Foo + current val.a value
// 1 + 42 == 43
val.a = val.Foo() + val.a;





Yeah I thought it was going to be 42 + 1 because a was modified in Foo(), but since the operation already had a 0 before calling Foo() the operation was going to be 0+1 instead of 42+1. Thanks
– pnadalini
Aug 24 at 19:30


a


Foo()


Foo()



On Foo() you always return 1.


Foo()



Correct code:


int Foo()

a = a + 42;
return a;





You missed the point of the exercise, it is showing how the order of operations work, the intended code is correct, the demonstration is that a becomes 1, not 43 after it is called.
– Ron Beyer
Aug 24 at 19:14


a


1


43





Actually, your question says "the code returns 1 and not 43". The only return statement in your code is return 1;. It's pretty obvious why it happens. Was this an "exercise"; you wrote deliberately confusing code and asked people why it's behaving the way it is? And, if you want to know why the code is writing a particular value to the console, you should ask that, not talk about "return".
– Flydog57
Aug 24 at 19:53


return


return 1;






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế