C# 静态变量 - 范围和持久性

发布于 2024-11-06 21:48:32 字数 660 浏览 0 评论 0 原文

我只是做了一个小实验:

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;
  }
}

然后我运行:

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());

我期望的结果是 0, 3, 0, 10, 0。

令我惊讶的是,我得到了 0, 3, 3, 10, 10。

这些变化持续多久?程序执行的持续时间?函数调用静态方法的持续时间?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

江挽川 2024-11-13 21:48:32

它们将在 AppDomain< 的持续时间内持续存在/a>.对静态变量所做的更改在方法中可见。

MSDN:

如果使用 Static 关键字声明局部变量,则其生存期比声明该变量的过程的执行时间长。如果该过程位于模块内部,则只要应用程序继续运行,静态变量就会一直存在。

有关更多详细信息,请参阅以下内容:

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:

时常饿 2024-11-13 21:48:32

我预期的结果是 0, 3, 0, 10, 0。

令我惊讶的是,我得到了 0, 3, 3, 10, 10。

我不确定为什么你会期望静态变量恢复到其原始值从 Foo(int) 方法中更改后。静态变量将在进程的整个生命周期中保留其值,并且每个类仅存在一个静态变量,而不是实例。

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.

痴者 2024-11-13 21:48:32

如果它是静态变量,则意味着在程序运行期间它仅存在于内存中的一个位置。

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

像你 2024-11-13 21:48:32

根据 C# 规范,静态变量将在第一次将类加载到 AppDomain 时初始化,并且将一直存在,直到该 AppDomain 被卸载(通常是在程序终止时)。

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.

谁的年少不轻狂 2024-11-13 21:48:32

在程序执行期间。

静态类变量就像全局变量。它们没有连接到某个类的特定对象 - 每个程序只有一个实例。在函数执行期间唯一存在的变量是函数的自动(局部)变量。

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.

巴黎夜雨 2024-11-13 21:48:32

它在程序执行期间持续存在,或者直到您用另一个值覆盖它为止。如果你想让结果成为你想要的结果,你应该在构造函数中指定 myInt = 0 之前 return myInt;

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;

心头的小情儿 2024-11-13 21:48:32

只要您的应用程序存在,您对静态范围的更改就会一直存在

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

梦醒时光 2024-11-13 21:48:32

静态变量属于类型,而不属于其实例。通常(如果您没有创建多个应用程序域)类型对象仅加载一次并在进程的生命周期内存在。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文