C # static and non-static members,

Source: Internet
Author: User

C # static and non-static members,

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 to interpret the meaning of the C # static method.

 

Bytes --------------------------------------------------------------------------------------------------------------

Data Member

Data members can be divided into static variables and instance variables.
Static member: a static member variable is associated with a class and can be used as a "common" variable in the class (a common expression). It does not depend on the existence of a specific object, you can use the class name adding operator and variable name to access the service.
Instance Member: The instance member variable is associated with the object, and the access instance member variable depends on the existence of the instance.

Function Member

Methods can be divided into static methods and instance methods.
Static Method: static method is a method that does not belong to a specific object. Static methods can access static member variables. Static methods cannot directly access instance variables. In the case of instance function calls, the instance variables are passed as parameters to the static method. Static methods cannot directly call instance methods. They can be called indirectly. You must first create an instance of the class and then call static methods through this specific object.
Instance method: the execution of an instance method is associated with a specific object. Its execution requires an object to exist. The instance method can directly access static variables and instance variables. The instance method can directly access the instance method and static method. The static method can be accessed by adding a variable name to the class name operator. When multiple instance objects exist, the memory does not contain copies of a specific instance method. Instead, all objects of the same class share a copy of each instance method (the instance method only occupies one set of space ).

Static things are generated in the heap during construction and will not be released or changed before the end.
Because a large amount of data access has been generated and is not released, instances will not be repeatedly created in the stack to save time.
Non-static repeated call of methods may cause the used variables or methods to be recycled by the system and then created.
Static methods should be used for access operations with large data volumes to improve performance.
If a method runs well without an instance, it should have been static unless you intentionally write it as non-static, so the efficiency is nonsense.

Use Cases of static methods

1. This method has nothing to do with any instance.
2. This method has nothing to do with a specific instance.
3. This method is an operator.
4. This method does not modify instances.
However, except the first and third items remain unchanged, the second and fourth items are wise.

Static Methods

1. static declared variables and methods will be placed in the heap area of the memory, and space will be allocated even if not called, rather than static space will be allocated only when used, release after use.
2. static can be called without creating an instance.

When a class member is declared as static, it can be accessed before the class object is created and no object reference exists. Therefore, most static members are used for global purposes. You can declare both methods and variables as static. When loading a class, one static block after another is executed only once. That is to say, some common functions that are widely used in the system generally adopt static methods. Non-general functions can use non-static methods, because not many of them are used in the system, which is not as extensive as general functions. Instantiation is more suitable than static methods. It consumes less resources than static methods. Therefore, the static method is usually determined based on actual needs.

========================================================== ==============

To write a program in C # Or an oo language, you should first identify when static functions are used and when member functions are required.
If the difference is not made, the meaning of oo is abandoned, just to slightly improve the performance, then I suggest you do not use c #, write in C, you will get more performance improvement, it is not an order of magnitude.

Secondly, I will give a rough look at the memory consumption of static functions and static members, the performance improvement brought about, and the dangers.
For memory consumption, it generally seems that only static members will consume, but static functions will not.
However, to call a static function of a class, if the static members of the class are not initialized, they need to be initialized. That is to say, the static function is called to initialize the static members, that is, the memory loss is caused by a subtle influence.
For performance improvement,
First, let's talk about the calling of static functions in the following form:
Class Name. Method Name
The Calling procedure is roughly as follows:
1. Find the type table by Class Name
2. Find the function pointer to be called through the type table.
For member functions, the formula is as follows:
Object Name. Method Name
The Calling procedure is roughly as follows:
1. Find the type table address through the Object Name
2. Find the type table through the type table address
3. Find the function pointer to be called through the type table.
That is to say, calling a member function requires one more step than a static function. Here it can be said that the performance of the member function is inferior to that of the static function.

Finally, let's talk about the abuse of static functions for the sake of performance.
As I did in the previous example, instead of using static functions for static functions, the following forms are generated in large numbers:
Public class UglyClass
{
Public static void UglyMethod (UglyClass data );
}
Before that, let's talk about the data that can be accessed by static functions. There are roughly two types of data:
1. Static members;
2. parameters;
If you use static members to transmit data, you need to be careful. Especially in multi-threaded operations, operations that do not work well may interfere with each other, and this problem is hard to be found.
If you use parameters as the method to pass data,
First, the parameter list is bloated and should not have been passed in;
Second, the read and write operations of parameters are the same as those of static members, and mutual interference occurs during multi-threaded operations.
The potential harm of these static functions can be avoided to the member functions of the class.

 

Bytes ---------------------------------------------------------------------------------------------------------------

 

Static methods, static classes, static members, static variables, and static fields all belong to static members. static declared variables and methods will be placed on the memory stack, and memory space will be allocated even if they are not called, instead of static variables, memory space is allocated only during use and is released after use.

When a class member is declared as static, the class member is used before being instantiated or before any object is referenced. That is to say, the static member does not need to create an instance, you can call it directly. This static member is usually applicable to global member variables, so that you do not need to create instances multiple times during extensive calls. Of course, using static members also has some advantages and disadvantages, static members and methods can be called without the need to create an instance, but some attribute encapsulation is broken, reducing security.

When the program starts to run and the related classes are loaded, the static members are allocated memory space on the memory stack,

Non-static members, that is, instance members and attributes, allocate memory space for these attributes only when the object is attached to the instance. The memory occupied during use will gradually increase as the instance is created. Instance members are usually used in some less commonly used methods, functions.

 

------------------------------------------------------------------------

 

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.