Summary usage of static keywords in ASP. NET

Source: Internet
Author: User

We should be familiar with the static keyword, but many people do not understand the static keyword, which is actually not familiar with auto. The static keyword is not auto, and the variable is allocated during program initialization until the program exits.

Static variables

When writing a class, we are actually describing the attributes and behaviors of the object, but not generating a substantive object. Only through the new Keyword will the object be generated, at this time, the system will allocate memory space to the object, and its method can be used for external calls.

Sometimes, we hope that, no matter whether an object is generated or no matter how many objects are generated, some specific data has only one copy in the memory space. For example, all Chinese people have country names, every Chinese person shares the country name and does not have to assign a variable for the country name of the Code to every Chinese person's instance object. Example:

 
 
  1. Protected void Page_Load (object sender, EventArgs e)
  2. {
  3. // 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.
  4. Response. Write (Chinese. Country );
  5. }
  6.  
  7. Class Chinese
  8. {
  9. Public static stringCountry="China";
  10. Public void getCountry ()
  11. {
  12. // The member methods in the class can also directly access static member variables
  13. HttpContext. Current. Response. Write ("Hello! "+ Country );
  14. }
  15. }

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

 
 
  1. Public void getCountry ()
  2. {
  3. // The member methods in the class can also directly access static member variables
  4. HttpContext. Current. Response. Write ("Hello! "+ Country );
  5. Static intI=1;
  6. }

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:

 
 
  1. Protected void Page_Load (object sender, EventArgs e)
  2. {
  3. Chinese. getCountry ();
  4. }
  5.  
  6. Class Chinese
  7. {
  8. Public static void getCountry ()
  9. {
  10. HttpContext. Current. Response. Write ("Hello! China ");
  11. }
  12. }

Note: In a static method, only other static members of the same type can be called directly, including variables and methods. This is because for non-static methods and variables, you need to create an instance object for the class before using it, and the static method does not need to create any objects before using it.

Const and static readonly are indeed very similar: access through the class name rather than the object name, read-only in the program, and so on. In most cases, they can be mixed.
The essential difference between the two is that the const value is determined during compilation, so it can only be specified through a constant expression during declaration. Static readonly calculates its value during running, so it can be assigned a value through a static constructor.

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 usually declare a variable:

 
 
  1. int a;   
  2. string s;  

Actually:

 
 
  1. auto int a;   
  2. auto string s;  

The static variable declaration is:

 
 
  1. static int a;   
  2. static string s;  

This seems to be more conducive to understanding that auto and static are paired keywords, just like private, protected, and public;

I don't understand the static keyword. In fact, I don't understand auto because it is more general. Some things you use every day, but it doesn't necessarily mean you really understand it; auto means that the program automatically controls the life cycle of the variable. It usually means that the variable is allocated when it enters its scope and is released when it leaves its scope; static is not auto, and variables are allocated during program initialization until the program exits. static is assigned to release variables according to the life cycle of the program, instead of the variable's own Lifecycle

  1. Comments on ASP. net web controls
  2. ASP. NET Control Learning Summary
  3. ASP. NET front-end controls comment: Avoid obsessive-compulsive disorder and rush to simple and efficient
  4. Shell functions in ASP. NET 2.0 Environment
  5. Batch insert data to the database in ASP. NET 2.0

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.