Comparison between static and non-static methods in C #

Source: Internet
Author: User

C # What are the differences between static and non-static methods? Let's use this article to explain it to you.

The C # class can contain two methods: the C # static method and the non-static method. So what are their definitions? What are their differences in use?

Let's take a look at the most intuitive difference: the static method uses the static modifier, and the non-static method is vice versa.

Let's take a look at the differences between the static and non-static methods in C # in four aspects:

C # comparison between static and non-static methods 1,
C # static members:
① Static members belong to the class and non-static members belong to the class instance.
② Each time a class instance is created, a new storage block is allocated to non-static members in the memory;

Static members belong to the class and are shared by instances of each class. No matter how many instances are created for the class, static members of the class only occupy the same region in the memory.

C # comparison between static and non-static methods 2,
C # static method
1. C # static methods belong to all classes and can be used before class instantiation.
2. Non-static methods can be used to access any member of the primary class. Static methods can only be used to access static members of the primary class.
3. Because static methods can be used before class instantiation, non-static variables in the class must be allocated after instantiation,

In this way, the memory address used by non-static variables cannot be determined when the C # static method is called. So it cannot be used. The static variable address is fixed for the class, so it can be used.

C # comparison between static and non-static methods 3,
C # static method is a special member method. It does not belong to a specific instance of the class, but to the class itself. Therefore, you do not need to create a class instance for the static method, but use the class name. Static Method format.
1. The static method is a member method in the class and belongs to the whole class, that is, it can be called directly without creating any object!
Only static variables and other static methods can appear in static! In addition, the keyword such as this... cannot be used in the static method because it belongs to the entire class!
2. The efficiency of static methods is higher than that of Instantiation. The disadvantage of static methods is that they cannot be destroyed automatically, but instantiated methods can be destroyed.
3. The static method and static variable always use the same memory after being created, and multiple memories will be created using the instance method.
4. There are two methods in C #: instance method and static method.

C # comparison between static and non-static methods 4,
C # obtain the class name in the static method
Static Methods:
String classname = system. reflection. methodbase. getcurrentmethod (). reflectedtype. fullname;

Non-static methods can also be used:
String classname = This. GetType (). fullname;

The difference between the C # static method and the non-static method is resolved to interpret the meaning of the C # static method. It is helpful for you to understand and learn the C # static method and the non-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.