Enter the C # static method

Source: Internet
Author: User


A while ago, the project had been using static methods or static variables, and I found some information on the Internet and made a small summary.


So what is a static method? The most intuitive static method uses static
Modifier. Static methods, as a special member method, can be seen as a method of the class itself, rather than a specific instance of the class. Therefore, you can use static methods without class instantiation. Generally, the format is class name. static method.

Class A {public static void B () {...} A. B (); // you can directly use

 


Due to the special nature of static methods, under what circumstances should we use static methods? How to use static fields or static construction methods in a program is usually applicable to data that does not change frequently and is frequently used. In this case, the static method is more efficient, such as connecting strings, configuration information and so on. When the preceding two points are met, a read operation will be performed once, which will be easy to use and save the managed resources, because for static members, A static field identifies only one storage location.

What is the difference between static and non-static methods in writing memory? Different non-static methods require multiple memories. After the static method is created, the same memory is used. In this way, the static method is more efficient. However, static methods cannot be self-destroyed. No matter whether these methods or attributes are used in the future, even if no one accesses the program, the memory will not be released. In addition, the data of the static attribute seen by all visitors is almost the same. For example, when user a sets the username attribute, the username is still set by user. If this feature is used in fixed data, there will be no major problems, such as connection strings.

What are the defects of static methods? When talking about the disadvantages, I personally know that there is a lack of object-oriented programming. Static methods do not support extension and polymorphism using abstract or virtual methods. In this way, software flexibility is greatly limited, so we should be careful when using it. If a method has nothing to do with the instance object of its class, it should be static, instead of writing it as an instance method. Of course, you can write all the instance methods as static, and pass the instance as a parameter. Generally, no problem may occur. From the object-oriented perspective, when you choose to use the instantiation method or static method, you should determine whether the method has a logical correlation with the instantiated object, if so, you should use the instantiated object instead of the static method. This is only from the object-oriented perspective. The instantiation method is recommended for thread security, performance, and compatibility.

I will introduce you to the usage of the C # static method. I hope it will be helpful for you to learn and use the C # static method.

 

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.