Asp.net static variable

Source: Internet
Author: User

Asp tutorial. net page is a class, we visit a page. An instance of this class will be instantiated on the server to respond to our requests.
In asp.net tutorial C #, the static variable indicates that the variable belongs to a static class, rather than an instance of the class. It can be said that all instances of this class share a static variable.


First, let's look at a test instance.

# Include <iostream>
# Include <iomanip>
Using std: cout;
Using std: endl;

Long next ();

Int main (){
For (int I = 0; I <30; I ++ ){
Cout <std: setw (12) <next ();
}
Cout <endl;
Return 0;
}

Long next (){
Static long last = 0;
Last = last + 1;
Return last;
}

Output result

1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26
27 28 29 30

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.

 

Static variables are used for calculation. The average running number is input by the user.

# Include <iostream>
Using namespace std;
 
Int f (int I );
 
Int main ()
{
Int num;
 
Do {
Cout <"Enter numbers (-1 to quit ):";
Cin> num;
If (num! =-1)
Cout <"average is:" <f (num) <"n ";

} While (num>-1 );
 
Return 0;
}
 
Int f (int I)
{
Static int sum = 0, count = 0;
 
Sum = sum + I;
 
Count ++;
 
Return sum/count;
}

The result is

Enter numbers (-1 to quit): 1
Average is: 1
Enter numbers (-1 to quit): 2
Average is: 1
Enter numbers (-1 to quit): 3
Average is: 2
Enter numbers (-1 to quit): 2
Average is: 2
Enter numbers (-1 to quit): 1
Average is: 1
Enter numbers (-1 to quit): 2
Average is: 1
Enter numbers (-1 to quit): 3
Average is: 2
Enter numbers (-1 to quit):-1

Initialize static member field declaration outside the class

# Include <iostream>
Using std: cout;
Using std: endl;

Class Box {
Public:
Box (){
Cout <"Default constructor called" <endl;
++ ObjectCount;
Size = width = height = 1.0;
}

Box (double lvalue, double wvalue, double hvalue ):
Length (lvalue), width (wvalue), height (hvalue ){
Cout <"Box constructor called" <endl;
++ ObjectCount;
}

Double volume () const {
Return length * width * height;
}


Int getObjectCount () const {return objectCount ;}

Private:
Static int objectCount;
Double length;
Double width;
Double height;
};
Int Box: objectCount = 0;

Int main (){
Cout <endl;

Box firstBox (17.0, 11.0, 5.0 );
Cout <"Object count is" <firstBox. getObjectCount () <endl;
Box boxes [5];
Cout <"Object count is" <firstBox. getObjectCount () <endl;

Cout <"Volume of first box ="
<FirstBox. volume ()
<Endl;

Const int count = sizeof boxes/sizeof boxes [0];

Cout <"The boxes array has" <count <"elements ."
<Endl;

Cout <"Each element occupies" <sizeof boxes [0] <"bytes ."
<Endl;

For (int I = 0; I <count; I ++)
Cout <"Volume of boxes [" <I <"] ="
<Boxes [I]. volume ()
<Endl;

Return 0;
}

Result:

Box constructor called
Object count is 1
Default constructor called
Default constructor called
Default constructor called
Default constructor called
Default constructor called
Object count is 6
Volume of first box= 935
The boxes array has 5 elements.
Each element occupies 24 bytes.
Volume of boxes [0] = 1
Volume of boxes [1] = 1
Volume of boxes [2] = 1
Volume of boxes [3] = 1
Volume of boxes [4] = 1

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.