Static variable Scope

Source: Internet
Author: User
Tags thread variable scope
Variable

Static variable and asp.net
In C #, a static variable indicates that the variable belongs to a class, not an instance of the class. It can be said that all instances of the class share a static variable.
ASP.net's page is a class where we visit a page. An instance of the class is instantiated on the server to respond to our request.

"All instances share a static variable" This means that all clients are accessing the same variable as the static variable in the ASP.net page.

Because every time we visit the ASP.net page, we are an entirely new object, not the last one we visited. So we did not keep any changes to the variables in the page when we last page access. In the face of this problem, a lot of beginners ' intuition is to declare this variable as static, the test when they found that really retained the status of the page. There was no discovery of this and another mistake was introduced. Because all you want is a page that retains its state, and this state is for a client (the effect of the session). The result is that as long as a client changes the value, all other clients are affected (like the applicatin effect). In this case, the most likely need is a viewstate or session.

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

Strong type:
The object is preserved in the application and the cast action is required for its preservation and use. Box&unbox is more necessary for value types. has a greater impact on performance.
The static variable is a strongly typed object.

Thread synchronization:
Application all objects into a collection, which locks the collection to any object in the Access collection.
If there are application["a"], application["B"], application["C"], the thread access application["a"] other threads cannot access application["B"] and application["C"].
Static variables can be placed in different classes, depending on their role. This allows you to access different static variables in parallel without thread-safety issues.

Friendly tips:
1. For static variables, do lock. You can use Lock (typeof (classname)) to lock the type of class that the variable is in, to achieve thread synchronization.
2. Because Aplication,static member is a global variable, and we are writing programs in a multithreaded server environment, the use of them requires attention to thread safety 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.