For the static variable, we should be very familiar with the online introduction of it a lot, I will say my understanding.
Static English stationary, still. In C #, it is a class, method, property, field, constructor that is used to decorate. The official web can also modify operators, events for these two I seldom use. Reference, you do not have to instantiate the class.
Let me give you a few examples:
Variable before using static:
Demo.aspx: Defines static in the head,
1.protected static int i = 0;
2.protected void Page_Load (object sender, EventArgs e)
{if (! IsPostBack) {I++;label. text=i;}}
So when does 1 execute? The first time to enter this page will be triggered, because the static modified variable will only be initialized once, such as you run your program on the computer side, the first time to enter the Demo.aspx page when the trigger, and then execute Page_Load inside the content, at this time i=1.
What is the first time to enter the Demo.aspx page?
For example: I open a tab in the computer side input: http://192.168.1.150:81/Demo/Base/DemoStatic.aspx, this tab is called Tab 1, when the page is placed a label to record, then the value is: Label = 1;
I copy the link http://192.168.1.150:81/Demo/Base/DemoStatic.aspx Open in the New tab, called Tab 2, then: label=2, then I think Tab 1 is the first time to enter the Demo page.
So if I open the link on the mobile side, then: label=3. Why is that? Because this page has been opened, initialized once, whether you or I live for the first time other people enter, will not go to do 1.
Look:
private void Demo () {
static int a=1;
a++;
}
Similarly, after the first time, you will not execute the static int a=1.
Through the above points, I wrote a small demo hope to help.
can give someone else to write a project to see, such as I want to call a global variable, then how to write? And this variable is changed, when the change after the other person's acquisition is changed, then you can use the static.
This can be: take development, get token for example: in class Weixinconfig public static Tokenhelper Tokenhelper {private set; get;}
If I'm going to call it this way, WeixinConfig.TokenHelper.GetToken (), and Tokenhelper is also a class, this class is not static, but public class tokenhelper, which writes methods, public string GetToken () {}
This is the static modifier field and property, and it is a decorated class. So you're going to ask me, how do you change the value of token after the first time you execute it? Because it is very simple, I static is public static Tokenhelper Tokenhelper This class inside the public string GetToken () {}, then this time The GetToken method is also a method under a static class, so WeixinConfig.TokenHelper.GetToken () executes the GetToken method, but does not re-execute Public Static Tokenhelper tokenhelper This sentence, then you will say what is the use? I return the token value I want in the method, and this value is changed, and I use static to modify the Tokenhelper class, when the call is not instantiated, this is the role.
What is the change of state?
My understanding is that when there are no attributes in a class, only the method can be considered a class that is not state-independent. Recall that a non-static class instantiates an object, where is the purpose? The purpose is to save the state of the class through this instantiated object. All use static words, preferably in a state-independent class.
My understanding or static less use is not wonderful.
C # basic Knowledge Review-static