简体   繁体   English

ASP.NET中的静态C#类变量是什么?

[英]What are static C# class variables for in ASP.NET?

I know what static class variables do in a C++ class, what I'm not very clear about is the life-cycle of static class variables in a C# class used for an ASP.NET web app. 我知道静态类变量在C ++类中做了什么,我不太清楚的是用于ASP.NET Web应用程序的C#类中的静态类变量的生命周期。 Here's a code example: 这是一个代码示例:

namespace MyWebApp
{
    public static class MyFunctions
    {
        private static string _cachedID;

        public static string getID(string strValue)
        {
            if(_cachedID == null)
                _cachedID = strValue;

            return _cachedID;
        }
    }
}

Can someone explain it in plain English for me? 有人可以用简单的英语为我解释一下吗?

I've read somewhere. 我在某处读过。

A static variable/field comes into existence before execution of the static constructor for its containing type, and ceases to exist when the associated application domain ceases to exists. 静态变量/字段在执行其包含类型的静态构造函数之前就已存在,并且在关联的应用程序域不再存在时不再存在。

Since you are asking this question in the context of a multithreaded ASP.NET application, you should be extremely careful. 由于您在多线程ASP.NET应用程序的上下文中提出此问题,因此您应该非常小心。 Checkout the following scenario: 签出以下场景:

2 users Bob and Alice call the getID method at exactly the same time passing different arguments. 2个用户Bob和Alice在传递不同参数的同时调用getID方法。 Bob passes Foo and Alice passes Bar . Bob通过Foo ,Alice通过了Bar Since this is the first call, the _cachedID variable is not yet initialized so both enter the if condition, Bob with a slight delay. 由于这是第一次调用, _cachedID变量尚未初始化,因此两者都进入if条件,Bob稍有延迟。 So Alice sets the _cachedID static variable to Bar and a microsecond after, Bob sets it to Foo . 所以Alice将the _cachedID静态变量设置为Bar然后将其设置为微秒, Bob将其设置为Foo Now the code continues and the function returns Foo for both users. 现在代码继续,函数返回两个用户的Foo Bob of course is happy because that's what he wanted, but Alice wanted Bar . 鲍勃当然很高兴,因为那是他想要的,但爱丽丝想要Bar

For example if you wanted to perform a one time initialization in a multithreaded environment you might consider using the thread safe version of the Singleton Pattern . 例如,如果要在多线程环境中执行一次初始化,可以考虑使用Singleton Pattern的线程安全版本。

The moral of this is that you should be extremely careful when dealing with shared/static data in an ASP.NET application. 这样做的道理是,在ASP.NET应用程序中处理共享/静态数据时应该非常小心。 If you need to use it you need to properly synchronize the access to it or very bad things could happen. 如果您需要使用它,您需要正确地同步对它的访问,否则可能会发生非常糟糕的事情。 And they usually happen in production when your application is concurrently accessed by multiple users. 当您的应用程序被多个用户同时访问时,它们通常会在生产中发生。 On your local PC everything will work fine. 在您的本地PC上一切正常。

And back to your original question about the lifetime of a static fields: it is tied to the lifetime of the application domain. 回到关于静态字段生命周期的原始问题:它与应用程序域的生命周期相关联。

Classes which you can't and dont have to make an object of but you can only acces it from a static context. 您不能也不必创建对象的类,但您只能从静态上下文访问它。

you would use your example like this: 你会像这样使用你的例子:

MyFunctions.getID("bla");

http://msdn.microsoft.com/en-us/library/79b3xss3(v=vs.80).aspx http://msdn.microsoft.com/en-us/library/79b3xss3(v=vs.80).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM