This article mainly introduces C # static and non-static methods, focuses on the practical application of static methods, and helps to further deepen the understanding of C # program design, you can refer to the examples in this article to analyze the C # static and non-static methods, and perform a comprehensive analysis on their usage. Share it with you for your reference. The specific analysis is as follows:
Generally, the C # class can contain two methods: static method and non-static method.
The static method uses the static modifier, and the non-static method is vice versa.
A static method is a special member method. it does not belong to a specific instance of the class, but to the class itself. Therefore, you do not need to create a class instance for the static method, but use the class name. static method format.
1) the static method is a member method in the class and belongs to the whole class, that is, it can be called directly without creating any object.
Only static variables and other static methods can appear in static, and keywords such as this cannot be used in static methods because they belong to the entire class.
2) static methods are more efficient than instantiation. the disadvantage of static methods is that they are not destroyed automatically, while instantiated methods can be destroyed.
3) the static method and static variable always use the same memory after being created, and multiple memories will be created using the instance method.
4) Where can I use static fields and static constructor in a program? it is usually applicable to data that does not change frequently and is frequently used, such as connection strings and configuration information, when the preceding two points are met, a read operation can be performed once to facilitate the use of the service. at the same time, managed resources are saved, because for static members, A static field identifies only one storage location. No matter how many instances are created for a class, its static fields always have only one copy.
Static members exist in the memory. non-static members need to be instantiated to allocate memory. Therefore, static members cannot access non-static members. Because static members exist in the memory, non-static members can directly access static members in the class. There should be no problem in using static methods for common processing functions, which involves data sharing. static variable functions should be considered more, and static variables should be used with caution.
Static method:
The principle is to share the code segment;
Shared code segments do not cause any problems;
Because the code segment is used by the CPU as a "read", unless you maliciously "modify" the code segment at runtime;
Therefore, static methods can be safely used;
Static variables:
The principle is to share data segments;
As long as there is no "write" operation, there will be no problem, but the data is usually used for reading and writing, so pay attention to the use of static variables;
The following is a simple example of using static methods:
Using System; using System. collections. generic; using System. linq; using System. text; namespace ConsoleApplication1 {class Program {static void Main (string [] args) {int I = MyClass. add (7, 11); // call the static method Console. writeLine (I); Console. readKey () ;}} class MyClass {public static int Add (int x, int y) {return x + y ;}}}
Output result: 18
I hope this article will help you learn C # programming.