A C # static method is a special member method that does not belong to a specific instance of a class. Non-static methods can access any member of a class, while static can only access static members in a class. So how do we handle this feature of the C # static method? Let's take a look at a simple example of a C # static method application:
C # static Method instance Demo:
Class A
{
Int x;
Static int y;
Static int F(){
x=1; //Error, no access allowed
y=2; //correct, allow access
}
C # static Method application Example Analysis:
1. In this class definition, C # static method F () can access static member Y in the class, but cannot access non-static member X.
2, X as a non-static member, each instance of the class occupies a storage (or a copy), and the static method is shared by the class, and it cannot tell what class the current x belongs to, so it does not know which address in memory to read the value of the current x.
3, Y is a static member, all instances of the class have a common copy, static method F use it does not have any problems.
The example parsing of the C # static method is introduced to you here, and it is hoped that this example will give you an idea of the C # static method.