In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
ASP.NET to share a static variable example analysis, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.
In C #, the static variable indicates that the variable belongs to the class, not an instance of the class. It can be said that all instances of this class share a static variable.
The page of asp.net is a class, and we visit a page. An instance of this class will be instantiated on the server in response to our request. "all instances share a static variable," which means that the static variable in all asp.net pages accessed by the client is the same variable.
Because every time we visit the asp.net page, it is a brand new object, not the one we visited last time. So we didn't keep any changes to the variables on the page the last time we visited the page. When faced with this problem, the intuition of many beginners is to declare this variable as static, and they found that they really retained the state of the page during the test. While secretly overjoyed, I didn't find that another mistake had been introduced. Because all you want is for the page to retain its state, which is for a client (session effect). The result is that as long as one client changes the value, all other clients are affected (like the effect of Applicatin). In this case, it is very likely that what is needed is a ViewState or Session.
Application and static variables
Application is to save all objects through a collection.
Strongly typed: object is saved in Application, and cast action is needed to save and use objects. Box&UnBox is more required for value types. It has a great influence on the performance. The static variable is a strongly typed object.
Thread synchronization: Application puts all objects into a collection so that the collection is locked for any object that accesses the collection. If there are Application ["A"], Application ["B"], Application ["C"], threads accessing Application ["A"] other threads cannot access Application ["B"] and Application ["C"].
The static variable can be placed in a different class according to their role. This allows parallel access to different static variables without thread safety issues.
Friendly reminder:
1. For the static variable, when doing lock. The type of the class in which the variable is located can be locked through lock (typeof (classname)) to achieve thread synchronization.
two。 Because Aplication,static member is a global variable, and we write programs in a multithreaded server environment, we need to pay attention to thread safety when using them.
There is not much difference between asp.net and static in C++, in essence, they both exist at compile time in the address space. In other words, after your Build is successful, the space for this static variable (or class) already exists in the DLL file of the project, that is, the so-called instance of this variable / class already exists after your runtime system loads DLL, and is common to the application, so you can use it directly, while all pages use the same space.
You need to new an instance to use an ordinary class, because there is only a declaration but no definition in the application. When new is divided into a piece of space in the heap to give it an instance.
In a simple experiment, you can easily understand the difference between them: create a new simple project, add a class, add a variable to the class, compile, record the size of the dll file; then change the variable into a static variable (of course, your class must also become a static, think about why), compile, record the dll file size; compare the size of the two dll, is the latter one larger than the previous one?
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.