Introduction to static variable usage in asp.net

Source: Internet
Author: User
Tags httpcontext

Every time we access an 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 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

To understand static, you must first understand another relative keyword. Many people may not know this keyword, that is, auto. In fact, we usually declare variables without static modification, all are auto, because it is default, just as short and long are always int by default; we
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.


Example:

The code is as follows: Copy code

Protected void Page_Load (object sender, EventArgs e)

{  

// The format of "class name. Member" is used directly, and the format of "object name. Member" cannot be used. This is different from that of JAVA.

Response. Write (Chinese. Country );

}  

 

Class Chinese

 {  

Public static string Country = "China ";

Public void getCountry ()

{  

// The member methods in the class can also directly access static member variables

HttpContext. Current. Response. Write ("Hello! "+ Country );

}  


Note that the variables in any method body cannot be declared as static, as shown below:

The code is as follows: Copy code

Public void getCountry ()

{  

// The member methods in the class can also directly access static member variables

HttpContext. Current. Response. Write ("Hello! "+ Country );

Static int I = 1;


Static method

Sometimes we want to call a method without creating an object. In other words, this method does not have to be bound with an object. To achieve this effect, you only need to add the static keyword before the method defined in the class. We call this method a static member method, you can also access this static method like other static methods in the non-static member method of the class. For example:

The code is as follows: Copy code

Protected void Page_Load (object sender, EventArgs e)

{   

Chinese. getCountry ();

}  

 

Class Chinese

 {  

Public static void getCountry ()

{  

HttpContext. Current. Response. Write ("Hello! China ");

}  

A variable is usually declared:

The code is as follows: Copy code

Int;

String s;


Actually:

The code is as follows: Copy code

Auto int;

Auto string s;


The static variable declaration is:

The code is as follows: Copy code

Static int;

Static string s;


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.

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.