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:
- Class
-
- {
-
- Int X;
-
- Static int y;
-
- Static int F (){
-
- X = 1; // error. Access is not allowed.
-
- Y = 2; // correct, allow access
-
- }
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