C # differences between static and non-static methods,

Source: Internet
Author: User

C # differences between static and non-static methods,
Differences between static and non-static methods: 1. static methods can be called without class instantiation. Otherwise, non-static methods can be called only after being instantiated. 2. static methods can only access static members and methods. Non-static methods can be accessed. 3. static methods cannot be marked as override. As a result, the derived classes cannot be overwritten but can be accessed. 4. static members are initialized when they are used for the first time. When creating objects, non-static members are static and continuous in memory allocation. Non-static members are discrete in memory storage. Therefore, static and non-static methods are used, in terms of the call speed, the static method speed will be faster, because non-static methods need to be instantiated and allocated memory, but the static method does not need to be used. However, this speed difference can be ignored, regardless of when to use the static method, when to use the instance method: 1. A method has nothing to do with the instance object of its class, and does not depend on the new instance, so it should be static 2. if you do not need object-oriented idea, use static state. For example, if you want to use an animal> RUN, because you may expand the run state of a cat or a dog, you need to use non-static state methods for thread security: the static method has only one instance. When there is a static variable, when there is a high concurrency, the value of the static variable changes when all concurrent executions follow that method, this leads to a problem. Of course, concurrency insecurity does not matter with the method itself. It has something to do with the shared resource, such as a variable. Every thread can change its value by operating on it, multi-thread (concurrency is actually multi-thread). If the value of the operation is chaotic, the problem arises. Why should we use non-static methods?

This is a method I wrote. I wrote the static method directly at the beginning. There is no other reason, because I don't want to instantiate it. I have never thought about the static and non-static differences =! ~~; Of course, it is also possible to write a non-static method;

Cause

1. Because the method needs to be called repeatedly, the memory overhead will be incurred when the new method is used each time. The static method will remain in the memory, and the new space does not need to be used to initialize data;

2. Do not need to do OO;

3. The method does not depend on the new content;

The classic static method, which is basically the static method in DBHelp

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.