Comparison of C # static and Non-static methods

Source: Internet
Author: User
Tags comparison instance method

C # classes can contain two methods: C # static method and Non-static method. So what is the difference between their definitions? What difference do they make in using them? Let's take a look at the most intuitive difference: the static modifier is used as the method, and the opposite is a non-static method.

Here's a four-point view of the difference between C # static and Non-static methods:

C # Static and Non-static methods compare one, C # static members:

① static members belong to the class, and non-static members belong to instances of the class.

② each instance of a class is created, a new piece of storage is allocated in memory for non-static members;

Non-static members are owned by the class and are common to instances of each class, regardless of how many instances the class creates,

Static members of a class occupy only the same area of memory.

C # Static and Non-static methods compare two, C # static methods

1. C # static methods belong to all classes and can be used before class instantiation.

2. A non-static method can access any member of a class, and a static method can only access static members in a class.

3, because static methods can be used before class instantiation, and non-static variables in a class must be instantiated before memory can be allocated.

In this way, the memory address used by non-static variables cannot be judged by the C # static method invocation. So it can't be used. The address of the static variable is fixed to the class, so it can be used.

C # Static and Non-static methods comparison Three, C # static method is a special member method

It does not belong to a specific instance of a class, but to the class itself. Therefore, it is not necessary to create an instance of a class first in a static method, but rather with the class name. The format of the static method.

The 1.static method is a member method in a class that belongs to the entire class, that is, you can call it without creating any objects!

Only static variables and other static methods can appear inside static! And the static method cannot use this .... and other keywords ... Because it belongs to the whole class!

2. The static method efficiency is higher than the instantiation, the static method disadvantage is does not automatically destroys, but the instantiation can do destroys.

3. Static methods and static variables always use the same piece of memory after creation, but using instances creates multiple memory.

There are two kinds of methods in 4.c#: Instance method, static method.

C # Static and Non-static methods comparison Four, C # static method gets the name of the class

In 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 C # static and Non-static methods is designed to interpret the meaning of the C # static method, and I hope it helps you understand and learn about C # static and Non-static methods.

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.