157 recommendations for writing high-quality code to improve C # programs--Recommendation 100: Static and instance methods are no different

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.