.Net 静态类属性范围与运行时相关
假设我有两个 .Net 应用程序在一台计算机上运行。两个应用程序都访问类中的静态属性。按顺序考虑以下场景:
Application A
FooClass.MyStaticString = "A";
应用程序 B
FooClass.MyStaticString = "B";
应用程序 A
Console.WriteLine(FooClass.MyStaticString);
结果是“A”还是“B”?我只是好奇 .Net 静态究竟是怎样的。
Let's say that I have two .Net applications running on a single machine. Both applications access a static property in a class. Considering the following scenario in sequential order:
Application A
FooClass.MyStaticString = "A";
Application B
FooClass.MyStaticString = "B";
Application A
Console.WriteLine(FooClass.MyStaticString);
Would the result be "A" or "B"? I'm just curious how static .Net statics really are.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它们仅限于特定的 AppDomain。每个应用程序至少都有自己唯一的 AppDomain,因此静态属性/字段不会在应用程序之间共享。结果将是“A”。同样,如果您在一个进程中启动多个 AppDomain,静态属性/字段也不会在这些进程之间共享。
They are limited to the specific AppDomain. Each application at a minimum has its own unique AppDomain, so the static property/field is not shared across the applications. It would be "A" as a result. Similarly, if you fired up multiple AppDomains within one process, the static property/field would not be shared between those either.