Recommendation 100: There is no difference between static and instance methods
The static method is identical to the instance method at the time of loading and memory usage. Here, we first elicit a concept of "type Object". For example, type person, we all know that new person () produces an object called an "instance object" that is loaded onto the GC heap at run time. A type object, however, refers to the object that represents the person type itself, which is loaded onto the loader heap the first time the type is used. A type object includes its own pointer, its own synchronous index fast, a static field, and a method table. In this method table, both the static method and the instance method are stored, of course, the record entry for the method is stored, and the method itself is compiled by the runtime at the time of invocation. The distribution of type objects and instance objects in memory is as follows:
If you must say the difference between static and instance methods, the only difference between them is that when we need to use an instance method, we should first have an instance object. We cannot bypass the instance object and invoke the instance method directly from the type itself. So, from a design point of view, if a method is only related to the type itself, then it should be designed as a static method, and if it is related to an instance object of the type, it should be designed as an instance method.
Static methods are misunderstood by many people: static methods are naturally synchronous methods. Even programmers with a certain amount of development experience sometimes make this common-sense mistake. Although Microsoft claims that most of the code in the FCL is implemented as thread-safe, it does not mean that the code is naturally synchronous, and that for static methods to be thread-safe, the programmer must write synchronous code, rather than having the compiler or the runtime do it for us.
To understand static methods and instance methods from a design perspective. Leaving the design, they made no difference.
Turn from: 157 recommendations for writing high-quality code to improve C # programs Minjia
157 recommendations for writing high-quality code to improve C # programs--Recommendation 100: Static and instance methods are no different