C # example of static method concept Parsing

Source: Internet
Author: User
    C # the learning of static methods is not only a literal understanding of concepts, but also an understanding of actual operations. Here we will introduce you to the application example of C # static methods.

     

    C # static method is a special member method, which does not belong to a specific instance of the class. A non-static method can be used to access any member of the primary class, while a static method can only be used to access static members of the primary class. So how can we grasp the characteristics of the C # static method? Let's take a look at a simple C # Static Method Application Example:

     

    C # static method example:

     

 
 
  1. Class
  2.  
  3. {
  4.  
  5. Int X;
  6.  
  7. Static int y;
  8.  
  9. Static int F (){
  10.  
  11. X = 1; // error. Access is not allowed.
  12.  
  13. Y = 2; // correct, allow access
  14.  
  15. }

C # static method application instance analysis:

 

1. In this class definition, the C # static method F () can be a static member Y in the category, but cannot access non-static member X.

 

2. As a non-static member, X occupies a storage (or has a copy) in each instance of the class, while the static method is shared by the class, it cannot determine which class of instance X belongs to, so it does not know which address of the memory should be used to read the value of the current X.

 

3. Y is a static member, and all class instances share one copy. There is no problem when using static method F.

 

The example parsing of the C # static method is introduced here. I hope that you can understand the C # static method through this example.

 

 

 

 

 

 

 

 

Reprinted Source: http://developer.51cto.com/art/200908/147735.htm

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.