C # static member and method learning summary)

Source: Internet
Author: User

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 ).

 

If a member in the class is declared as static, the Member is called static member. In general, static members belong to the class, and non-static members belong to the class instance. Each instance that creates a class opens up an area for non-static members in the memory. The static members of the class are all of the classes, which are shared by all instances of the class. No matter how many copies are created for this class, a static member occupies only one region in the memory.

C #ClassStatic MemberThe life cycle of a variable is the time when it is created, and when it is destroyed. The "Lifetime" of a declared element is the time period that the element is available for use. A variable is a unique element with a lifetime. Therefore, the compiler treats process parameters and function return values as special cases of variables. The lifetime of a variable indicates the time period in which the variable can be retained. The value of the variable can be changed during the lifetime, but the variable always retains some values.

Different survival periods
Variables declared at the module level are usually usedProgram. Non-shared variables declared in a class or structure exist as independent copies of each instance that declares its class or structure. Each such variable has the same lifetime as its instance. However, the shared variable only has one lifetime, that is, the full duration of the application running.

Local variables declared with dim exist only when the process for declaring them is being executed. This also applies to process parameters and any function return values. However, if this process calls other processes, the values of local variables are retained during the call process.

Start of survival
When the local variable is declared, the survival time of the local variable starts. When the process starts, each local variable is initialized as the default value of its data type. Numeric variables (including byte and char) are initialized to 0, date variables are initialized to on January 1, January 1, and Boolean variables are initialized to false, the referenced type variables (including strings, arrays, and objects) are initialized to nothing.

Each member of the structure variable is considered as a separate variable initialization. Similarly, each element of the array variable is initialized separately.

If the variable is declared with an initial value setting item, the variable will be assigned a specified value when the variable declaration statement is executed, as shown in the following example:

Dim X as double = 18.973 'x had previusly been initialized to 0.
Variables declared in the internal block of the process are initialized as their default values when they enter the process. Whether or not the block has been executed, the initialization will take effect.

End of lifetime
When the process ends, the local variable value of the process is no longer retained, and the memory used by the local element is recycled. The next time you execute this process, all its local elements will be re-created and local variables will be initialized.

When an instance of a class or structure ends, its non-shared variables lose their values. Each new instance of a class or structure creates all its non-shared elements and initializes non-shared variables. The shared element is retained until the application stops running.

Extended lifetime
If a local variable is declared with the static keyword, its lifetime is longer than the execution time of the declared process. If the process is in a module, the static variable always exists as long as the application continues to run.

If the static variable is declared in the internal process of the class, the lifetime of the variable depends on whether the process is shared. If this process is declared with the shared keyword, the lifetime of the variable continues until the end of the application. If this process is not shared, its static variable is a member of the class instance, and its lifetime is the same as that of the class instance.

In the following example, the runningtotal function calculates the total traffic by adding new values to the total of previous values stored in the static variable applessold:

Function runningtotal (byval num as integer) as integer
Static applessold as integer
Applessold = applessold + num
Return applessold 'applessold keeps its current value.
End Function
If applessold is declared without static, the previous accumulated values are not retained during the function call, and the function only returns the same value for the previous call.

Declare applessold at the module level to produce the same lifetime. However, if you change the range of a variable in this way, this process will no longer have exclusive access to the variable. Because other processes can access the variable and change its value, the total traffic is unreliable andCodeIt may be more difficult to maintain.

static member is initialized before the first access and before any static Constructor (for example, call. To access a static class member, use the class name instead of the variable name to specify the position of the Member.

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.