The difference between static and non-static methods in C #

Source: Internet
Author: User
The difference between static and non-static methods: 1. Static methods can be called without a class instantiation, whereas non-static methods need to be instantiated before they are invoked; 2. Static methods can access only static members and methods, non-static methods are accessible; 3. Static methods cannot be marked as override, resulting in a derived class that cannot be overridden. but can be accessed; 4. Static members are initialized on first use. Non-static members are in the creation of objects, from the memory allocation of static is continuous, non-static in memory is discrete, so static methods and non-static methods, at the call speed, static method speed must be fast, because non-static methods need to instantiate, allocate memory, but static methods do not, However, this difference in speed can be ignored when using a static method, and when to use the instance method: 1. A method is independent of the instance object of his class and does not depend on the new instance, then it should be static 2. No need for object-oriented ideas to use static, such as animal-RUN,  Because you may expand the cat run, dog run, this time you need to use a non-static method about thread safety: Static method Only one instance, when there is a static variable, for high concurrency, the concurrency of all the execution of the method, the value of the static variable will change, resulting in problems; Of course, concurrency is not safe with the method itself, and the shared resource has a relationship such as a variable, each thread to operate on it, can change its value, multithreading (concurrency is actually multithreaded), operation it, its value confusion, there is a problem

The difference between static and non-static methods in C #

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.