'static' value appears to reset after function call

EmerG :

I found a lot of articles about statics (MSDN, MSDN 2, Stack Overflow, and lot lot more), but I still can't understand why this code returns -1:

class Program
{
    static int value = 0;

    static int foo()
    {
        value = value - 7;
        return 1;
    }

    static void Main(string[] args)
    {
        value -= foo();
        Console.WriteLine(value);
        Console.ReadKey();
    }
}

Here's what the debugger shows after foo() has run, but before the result is subtracted from value:

foo=1, value=-7

But one step later, value is -1:

value = -1

I would expect -8 because of the static field which is stored in memory once.

When I changed it to

var x = foo();
value -= x;

it shows -8

How does this work exactly?

shingo :

This problem is not about static; it's about how the subtraction works.

value -= foo(); can be expanded to value = value - foo()

The compiler will explain it into four steps:

  1. Load the value of value onto the stack.
  2. Call the method foo and put the result onto the stack.
  3. Do subtraction with these two values on the stack.
  4. Set the result back to value field.

So the original value of value field is already loaded. Whatever you change value in the method foo, the result of the subtraction won't be affected.

If you change the order to value = - foo() + value, then the value of value field will be loaded after foo is called. The result is -8; that's what you are expected to get.

Thanks for Eliahu's comment.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Properties values stored after static function call

From Dev

Is it possible to reset static field after function end or out of the scope?

From Dev

Angular validation messages appears after reset() form

From Dev

Reset after Jquery function

From Dev

How do I reference a static data in C that appears after the function that needs it?

From Dev

FullCalendar - dayRender appears not working after a 'removeEvents' call

From Dev

Does a static variable reset after a time

From Dev

Vue: Reset data object to original value after API call has failed

From Dev

ARM PC value after Reset

From Dev

Visibility of a function's return value after a call to fork()

From Dev

how to call function in component after change value in store?

From Dev

After function call, argument pointers don't keep their value

From Dev

PyQt lineEdit on textEdited signal value lost after function call

From Dev

How do I return function after getting the value in http call?

From Dev

I cannot assign a value to a variable after call a function in javascript

From Dev

How to call static function in a static class JS?

From Java

Dynamically call a static function in Java

From Dev

Call static function from variable

From Dev

Call template provided (static) function

From Dev

Call static function on array of types

From Dev

Order of static initialization with function call

From Dev

Reset / disable infinite scroll after AJAX call

From Dev

Controller is not being reset after href call

From Dev

bigcache is getting reset after each set call

From Dev

Javascript textarea value only appears after focus?

From Dev

JButton appears after selection of value in JList

From Dev

Parentheses after function call

From Dev

Call function after setInterval

From Dev

Why does the pointer not reset, in a recursive function call

Related Related

  1. 1

    Properties values stored after static function call

  2. 2

    Is it possible to reset static field after function end or out of the scope?

  3. 3

    Angular validation messages appears after reset() form

  4. 4

    Reset after Jquery function

  5. 5

    How do I reference a static data in C that appears after the function that needs it?

  6. 6

    FullCalendar - dayRender appears not working after a 'removeEvents' call

  7. 7

    Does a static variable reset after a time

  8. 8

    Vue: Reset data object to original value after API call has failed

  9. 9

    ARM PC value after Reset

  10. 10

    Visibility of a function's return value after a call to fork()

  11. 11

    how to call function in component after change value in store?

  12. 12

    After function call, argument pointers don't keep their value

  13. 13

    PyQt lineEdit on textEdited signal value lost after function call

  14. 14

    How do I return function after getting the value in http call?

  15. 15

    I cannot assign a value to a variable after call a function in javascript

  16. 16

    How to call static function in a static class JS?

  17. 17

    Dynamically call a static function in Java

  18. 18

    Call static function from variable

  19. 19

    Call template provided (static) function

  20. 20

    Call static function on array of types

  21. 21

    Order of static initialization with function call

  22. 22

    Reset / disable infinite scroll after AJAX call

  23. 23

    Controller is not being reset after href call

  24. 24

    bigcache is getting reset after each set call

  25. 25

    Javascript textarea value only appears after focus?

  26. 26

    JButton appears after selection of value in JList

  27. 27

    Parentheses after function call

  28. 28

    Call function after setInterval

  29. 29

    Why does the pointer not reset, in a recursive function call

HotTag

Archive