The difference between C # static and non-static methods is not only conceptual, but what are their specific differences? Let's make a resolution to you through this article.
C # classes can contain two methods: C # static and non-static methods. So what is the difference in their definition? What is the difference in their use?
Let's take a look at the most intuitive differences: The static method is used with the static modifier, and the inverse is a non-static method.
Let's look at the difference between a C # static and a non-static method in four ways:
C # static and non-static methods compare one,
C # static Members:
① static members belong to the class, and non-static members are owned by instances of the class.
② each instance of a class is created, a new piece of storage is allocated to the non-static member in memory;
Static members are owned by the class and are common to instances of each class, regardless of how many instances the class creates, and static members of the class occupy only the same area in memory.
C # static and non-static methods comparison Two,
C # static methods
1. C # Static methods are class-owned and can be used before class instantiation.
2. Non-static methods can access any member of the class, and static methods can only access static members in the class.
3. Because static methods can be used before the class is instantiated, non-static variables in the class must be instantiated before the memory is allocated.
In this way, C # static method calls cannot determine the memory address used by non-static variables. So it can't be used. The address of the static variable is fixed to the class, so it can be used.
C # static and non-static methods comparison of three,
A C # static method is a special member method that is not part of a specific instance of a class, but rather belongs to the class itself. Therefore, the static method does not need to create an instance of a class first, but instead takes the class name. The format of the static method.
The 1.static method is a member method in a class that belongs to the entire class, that is, you can call it directly without creating any objects!
Static variables and other static methods can only appear inside static! And this is not available in the static method .... and other keywords. Because it belongs to the whole class!
2. Static methods are more efficient than instancing, and the disadvantage of static methods is that they are not destroyed automatically, while instantiated can be destroyed.
3. Static and static variables always use the same piece of memory after they are created, and the way the instance is used creates multiple memory.
There are two methods in 4.c#: Instance method, static method.
C # static and non-static methods of comparison four,
Get the name of a class in C # static method
Static methods:
String className = System.Reflection.MethodBase.GetCurrentMethod (). Reflectedtype.fullname;
Non-static methods can also be used:
String className = this. GetType (). FullName;
The difference between C # static and non-static methods is interpreted to explain the meaning of C # static methods, and it is hoped to help you understand and learn C # static and non-static methods.
Comparison of static and non-static methods in C #