1. Introduction
The method using the static modifier is a static method, and vice versa is a non-static method. 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, there is no need to create an instance of the class first for static methods, but instead adopt the format of the class name. static method. In addition, there are the following differences between static methods and non-static methods:
1. Static methods can be called without class instantiation, otherwise non-static methods can only be called after instantiation;
2. Static methods can only access static members and methods, and non-static methods can access them;
3. Static methods cannot be marked as override, resulting in the derived class being unable to be overwritten, but can be accessed;
4. Static members are initialized when used for the first time. Non-static members are continuous in terms of memory allocation when creating objects. The non-static members are discrete in memory storage. Therefore, static methods and non-static methods will definitely be faster in terms of calling speed, because non-static methods need to be instantiated and allocated memory, but static methods are not used, but this speed difference can be ignored.
A method is a member method in the class. It belongs to the entire class. It can be called directly without creating any object. Only static variables and other static methods can appear within static. Moreover, keywords such as this... are not used in the static method because they belong to the entire class!
Static method: The principle is to share code segments. Shared code segments will not cause any problems. Because code segments are used for "reading" by the CPU, unless you maliciously "modify" the runtime code segments, so the static method can be used with confidence.
Static variables:The principle is to share data segments. The same as above. As long as there is no "write" operation, there will be no problem. However, data is usually used for reading and writing, so static variables should be used with care.
2. Code cases
public class A { public static void Method1() { ("I am Method1, a static method"); //Get the class names in static methods string className = ().; ("Method1The method class:{0}", className);// Namespace. Class name } public void Method2() { ("I'm Method2, not a static method"); //Non-static method to get class name string className = ().FullName; ("Method2The method class:{0}", className);// Namespace. Class name; the same class as the above method, so the output result is the same } } class Program { public static void Method3() { ("I'm Method3, not a static method"); } static void Main(string[] args) { A.Method1(); // A.Method2(); // Report an error, non-static methods must be instantiated before they can be referenced A a = new A(); a.Method2(); Method3(); } }
3. Summary
The static principle is:
1. Ensure that there will be no concurrency.
2. Make a measure of convenience and speed and development difficulty.
This is all about this article about C# static methods. I hope it will be helpful to everyone's learning and I hope everyone will support me more.