37

I just did a little experiment:

public abstract class MyClass
{
  private static int myInt = 0;

  public static int Foo()
  {
    return myInt;
  }

  public static int Foo(int n)
  {
    myInt = n;
    return bar();
  }

  private static int bar()
  {
    return myInt;
  }
}

and then I ran:

MessageBox.Show(MyClass.Foo().ToString());
MessageBox.Show(MyClass.Foo(3).ToString());
MessageBox.Show(MyClass.Foo().ToString());
MessageBox.Show(MyClass.Foo(10).ToString());
MessageBox.Show(MyClass.Foo().ToString());

The results I expected were 0, 3, 0, 10, 0.

To my surprise, I got 0, 3, 3, 10, 10.

How long do these changes persist for? The duration of the program execution? The duration of the function calling the static method?

Ozzah
  • 10,631
  • 16
  • 77
  • 116

9 Answers9

51

They will persist for the duration of AppDomain. Changes done to static variable are visible across methods.

MSDN:

If a local variable is declared with the Static keyword, its lifetime is longer than the execution time of the procedure in which it is declared. If the procedure is inside a module, the static variable survives as long as your application continues running.

See following for more details:

Chris F Carroll
  • 11,146
  • 3
  • 53
  • 61
YetAnotherUser
  • 9,156
  • 3
  • 39
  • 53
29

The results I expected were 0, 3, 0, 10, 0.

To my surprise, I got 0, 3, 3, 10, 10.

I'm not sure why you would expect the static variable to revert back to its original value after being changed from within the Foo(int) method. A static variable will persist its value throughout the lifetime of the process and only one will exist per class, not instance.

Community
  • 1
  • 1
Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • 2
    +1 for distinguishing between class variables and instance variables. – Cylon Cat May 13 '11 at 00:38
  • I think he was assuming that the `myInt = 0` part would be evaluated more than once. – Chris Eberle May 13 '11 at 00:44
  • 1
    @Chris - he? are you sure? ;) – YetAnotherUser May 13 '11 at 00:53
  • @YetAnotherUser yes, Chris was right... he (I) did assume that. Since the static class is never instantiated, I just assumed that it would be used, an int is returned, and then the program would have forgotten it was ever used in the first place. – Ozzah May 13 '11 at 01:20
9

If it's a static variable, that means it exists exactly one place in memory for the duration of the program.

Chris Eberle
  • 47,994
  • 12
  • 82
  • 119
5

Per the C# spec, a static variable will be initialized no later than the first time a class is loaded into an AppDomain, and will exist until that AppDomain is unloaded - usually when the program terminates.

Ben
  • 6,023
  • 1
  • 25
  • 40
4

For the duration of the program execution.

Static class variables are like globals. They're not connected to specific objects of a class - there's only one instance of those per program. The only variables that live during function execution time are automatic (local) variables of the function.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
2

They persist "for the lifetime of the application domain in which your program resides" according to Microsoft Docs: Static Classes and Static Class Members (C# Programming Guide).

See also:

weir
  • 4,521
  • 2
  • 29
  • 42
1

It persist for duration of the program execution, or until you overwrite it with another value. If you want to make the result as what you want it to be, you should specify myInt = 0 in the constructor before return myInt;

Viken Ong
  • 109
  • 2
  • 10
0

Your changes in static scope will live as long as your app

Marek
  • 1,688
  • 1
  • 29
  • 47
0

Static variables belong to type, not to its instance. And usually (if you are not creating multiple app domains) type objects are loaded only once and exist during the lifetime of the process.

Andrew Bezzub
  • 15,744
  • 7
  • 51
  • 73