静态变量在 ASP.NET 页面中的什么位置工作?
今天去面试,一切都很顺利,但是后来面试官问了我一个问题静态变量在 C# 中工作的地方 - 在应用程序级别还是在页面级别。
我对这个答案不是很清楚,因为我只知道静态变量存储在堆上,并且对网络相关的事情一无所知。
然后他试图通过举一个例子让我更清楚,在一个页面中我使用静态变量,三个用户正在访问该页面,其中一个用户更新了静态变量的值,其余两个用户可以看到什么值复制或更新将得到反映。
I had an interview today and every thing was going very good, but then an interviewer asked me a question Where Does Static Variable Work in C#- At Application Level or At Page Level.
I was not very much clear about this answer as I only knew that static variables are stored on heap and I didn't knew anything about web related thing.
Then he tried to make me more clear by giving an example that in a page I am using static variable and three users are accessing the page one of the user updates the value of static variable, What value will be visible to remaining two users an old copy or the update will be reflected.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
除非是
[ThreadStatic]
,否则静态变量对于每个 AppDomain 仅有一个值。在ASP.Net中,每个应用程序都有自己的AppDomain,因此静态变量将被应用程序中的所有请求共享。这就是面试官的意思——在 ASP.Net 应用程序中使用静态变量是一个常见错误,当多个请求同时发生时,可能会导致神秘的损坏错误。
Unless it's
[ThreadStatic]
, a static variable will only have one value for each AppDomain.In ASP.Net, each application has its own AppDomain, so static variables will be shared by all requests in the application. This is what the interviewer was getting at – using
static
variables in ASP.Net applications is a common mistake that can lead to mysterious corruption errors when multiple requests happen at once.当某一页更改值后,其他页都会获得更新后的值。
这可能是也可能不是您想要的。这就是为什么静态变量在网络编程中是危险的。例如,在 Winforms 应用程序中,静态变量可以很好地存储该进程的全局值,因为可能只有一个进程正在运行。您会得到预期的行为。
然而,在 Web 应用程序中,您的代码可以在同一个 AppDomain。当价值被共享时,开发人员有时会感到惊讶。
如果您希望这些值不同(通常会这样做),您可以使用ThreadStatic 属性。不同的网络请求位于不同的线程中,因此它们将保持彼此不知道的状态。我从不使用它,因为我不相信垃圾收集会在下一个页面调用之前删除该值,这可能会重用同一线程。同样,我不相信静态变量可以在 ASP.NET 线程之间有目的地共享值;使用服务器变量。
After one page changes the value, the other pages would all get the updated value.
This may or may not be what you want. This is why static variables are dangerous in web programming. In a Winforms application for example, a static variable works fine to store values that are global for this process, as there is likely just one process running. You get the expected behavior.
In a web application however, your code can be started in multiple threads in the same AppDomain. Developers are sometimes surprised when the value is shared.
If you want the values to be different (you usually do), you can force this using the ThreadStatic attribute. Different web request are in different threads, so they will remain ignorant of each other. I never use this since I don't trust garbage collection to get rid of the value before the next page call, which might reuse the same thread. Likewise, I wouldn't trust static variables for purposefully sharing values between asp.net threads; use a server variable.
C# 和 ASP.NET 中的静态变量在应用程序级别工作。
至于他们将获得什么值,这取决于他们是否在页面更新静态变量之前或之后访问该变量。如果他们在静态变量更改之前获取值,他们将看到旧值。如果他们在静态变量更改后获得值,他们将获得新值。静态变量在 ASP.NET 中可能会很麻烦,我建议它们仅用于常量值或只读、不可变的类。
Static variables in C# with ASP.NET work at the application level.
As far as what value they would get, it depends on if they access the variable before or after the page updates the static variable. If they get the value before the static variable is changed, they will see the old value. If they get the value after the static variable is changed, they will get the new value. Static variables can be troublesome in ASP.NET, I would suggest they only be used for constant values or for read-only, immutable classes.