ASP. NET: all instances share a static variable.

Source: Internet
Author: User

In C #, the static variable indicates that the variable belongs to the class, not the instance of the class. It can be said that all instances of this class share a static variable.

The asp.net page is a class, and we access a page. An instance of this class will be instantiated on the server to respond to our requests. "All instances share a static variable", which means that the static variable on the asp.net page accessed by all clients is the same variable.

Every time we access the asp.net page, it is a brand new object, not the object we accessed last time. Therefore, the changes to the variables on the page were not retained during the last page access. When this problem occurs, many beginners intuitively declare this variable as static. During the test, they find that the page status is actually retained. I did not find another mistake. Because all you need is that the page can be retained, and this status is for the effect of a client session ). The result is that as long as one client changes this value, all other clients are affected, just like Applicatin ). In this case, it is very likely that a ViewState or Session is required.

Application and static variables

Application stores all objects in a collection.

Strong type: the object is saved in the Application, and the cast action is required to save and use the object. Box & UnBox is required for value types. It has a great impact on performance. Static variables are strongly typed objects.

Thread Synchronization: The Application puts all objects in a set, so that any objects in the access set will be locked in this set. Assume that Application ["A"], Application ["B"], Application ["C"], some threads access Application ["A"] other threads cannot access Application ["B"] and Application ["C"].

Static variables can be placed in different classes according to their functions. In this way, different static variables can be accessed in parallel without thread security issues.

Tip:

1. lock the static variable. You can use lock (typeof (classname) to lock the type of the class where the variable is located to achieve thread synchronization.

2. Due to Aplication, static member is a global variable, while we write programs in a multi-threaded server environment. For their use, we need to pay attention to thread security issues.

Static in asp.net and C ++ is not much different. In essence, they all exist in the address space during the compilation period. In other words, after you Build the project, the DLL file of the project already has the space for this static variable or class, that is to say, after the system loads the DLL during your runtime, the so-called instance of this variable/class already exists and is shared by applications, so you can directly use it, at the same time, all pages use the same space.

You need a new instance to use a common class. The reason is that only declarations are not defined in the application. When new is used, a space is allocated to one of its instances in the heap.

In a simple experiment, you can easily understand the differences between them: create a simple project, add a class, add a variable to the class, compile and record the dll file size; then change the variable to static variable. Of course, your class must also be static. Think about the reason), compile and record the dll file size, and compare the two dll sizes, is it bigger than the previous one?

  1. ASP. NET array basics: declarations, representations, and examples
  2. Top 10 skills in ASP. NET Programming II)
  3. Top 10 skills in ASP. NET Programming 1)
  4. ASP. net mvc tutorial (1): Preparations
  5. Deployment of ASP. net mvc Beta

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.