Static variables on the asp.net page

Source: Internet
Author: User

Asp tutorial about static variables on the. net page

Static variables are initialized only when they are used for the first time (may be earlier than or later than the Main function). The global variables in c exist earlier than the main function.
Static has modifiers, and global variables do not.
The static mechanism is much more complex, and its initialization order may cause competition (for example, when both static members of the two classes depend on each other). The global variables do not have these problems.


The test code is as follows:

View Code public partial class _ Default: System. Web. UI. Page
{
Private static string str;
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
Str = "this is a static variable value assignment" + DateTime. Now;
}
}
Protected void button#click (object sender, EventArgs e)
{
This. TextBox1.Text = str;
}
}

 

 

I used Firefox to open three pages in a row and click the button to display the same value (assign the connection value to IE and then change it)

View Code public partial class _ Default: System. Web. UI. Page
{
// Private static string str;
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
// Str = "this is a static variable value assignment" + DateTime. Now;
ViewState ["Key"] = "this is a static variable value assignment" + DateTime. Now;
}
}
Protected void button#click (object sender, EventArgs e)
{
This. TextBox1.Text = ViewState ["Key"]. ToString ();
}
}

 

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.


Changed to ViewState.


Static variables of non-generic classes and non-ThreadStatic exist only one copy in an AppDomain.
In other words, the same static variable in different AppDomains can be different.
The static variable of the generic class is one for each specific type
The static variable of ThreadStatic is one for each thread.

Whether the access is allowed depends on the access modifier of the variable and the access modifier of the class to which the variable belongs.
Static variables do not exist all the time. clr will help you prepare them before using this variable.
Therefore, you do not know whether the variable exists before using it. The only thing you can know is that it must exist (of course it may be null) during use)
The difference between BeforeFieldInit and BeforeFieldInit when clr prepares this static variable

Related Article

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.