STATIC variable scope

xiaoxiao2021-03-05  36

The Static variable is in the C # in C #, and 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. ASP.NET's page is a class, we visit a page. An instance of such a class will be instantiated on the server to respond to our request.

"All instances share a static variable" This means that all the Static variables in the ASP.NET page accessed all clients are the same variable.

Since we have access to the ASP.NET page, it is a brand new object, not the object we have last visited. So our last page is accessed, we have no reservation in the changes in the page. When I met this problem, many beginners' intuition was to declare this variable as Static, and I found it really retained the state of the page when I was tested. I didn't find this in addition to this happiness. Because you want only the page can retain the status, and this state is for a client (the effect of the session). The result is that as long as one client changes all other clients that have been influenced (like Applicatin). In this case, it is possible to have a viewState or session.

Application and Static Variable Application is to save all objects through a collection.

Strong Type: Saved in Application is Object, saving and use of objects to be CAST action. Box & Unbox is required for value types. There is a big effect on performance. The Static variable is a strong type of object.

Thread synchronization: Application puts all objects into a collection, so that any objects in the access collection will lock this collection. If there is Application ["A"], Application ["B"], Application ["C"], there is a thread access Application ["a"] other threads cannot access Application ["B"] and Application ["C"]. And Static variables can be placed in different clas based on their roles. This can access different static variables in parallel without thread security issues.

Friendly Tips: 1. When you do LOCK for Static Variables. You can lock the type of the class in this variable by Lock (TypeOf (ClassName) to achieve the purpose of thread synchronization. 2. Due to the APLICATION, Static Member is a global variable, and we are in multi-threaded server ambient write programs, which requires a problem with thread security.

转载请注明原文地址:https://www.9cbs.com/read-34680.html

New Post(0)