C# Static variables - scope and persistence

前端 未结 9 1038
一向
一向 2020-11-27 05:38

I just did a little experiment:

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

  public static int Foo()
  {
    return myInt;
  }

  publi         


        
9条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-27 06:06

    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;

提交回复
热议问题