ASPnet static variable

Source: Internet
Author: User

A static variable is defined in a class and is only in the same execution context. All users access the same variable. That is to say, a static variable is defined in a class. All users access the same variable. The scope of static variables is obviously at the application level. From this perspective, the static
Userid variable. User A logs on to the system and assigns a value to userid A and userid = A. Before user a publishes information, the value of userid remains unchanged. User B logs on to the system. Similarly, assign a value to userid B. Because the static value is at the application level, all users share this static variable. Therefore, the value of userid is no longer a but B. Then, user a submits the publishing information, and the userid of user a is B. Therefore, the user signature displayed in the end is no longer a but B, resulting in confusion.

This makes it clear that static variables are not allowed at the page level and normal variables are used for operations. When viewstate is used together, the problem is solved;

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 and retain the page status. Because all you need is that the page can be retained, and this status is for a client (Session effect ). The result is that as long as one client changes this value, all other clients are affected (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. The cast action must be performed 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:

Application puts all objects in a set, so that the set will be locked for any objects in the access 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. Lock (typeof (classname) is not necessarily suitable for some scenarios.

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.

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.