Introduction to 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 class instantiation, whereas non-static methods need to be instantiated before they are invoked;

2. Static methods can only access 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 the first time they are used. 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, But this difference in speed can be negligible.

When to use a static method, when to use an 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. Do not need the object-oriented concept when using static, such as the 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 methods have only one instance, when there are static variables, for high concurrency, all concurrent execution go that way, the value of static variables 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

Why use non-static methods

This is I write a method, originally directly written static method, no other reason, because do not want to instantiate can be less one line of code, and did not think of static and non-static difference = =! Of course it is possible to write a non-static method;

Reason

1. Because the method requires repeated calls, each time the new one will cause memory overhead, the static method will always be in memory, do not need to re-new a piece of space to initialize the data;

2. Do not need to do oo;

3. The method does not rely on new content;

Compared to the classic static method, Dbhelp is basically a static method

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.