C # static data member

Source: Internet
Author: User

① 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 (dynamic) State members in the urban 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.

Static data members are different from dynamic data members. Static variables are not stable, but static variables are declared, regardless of whether they are assigned a value or not. City creden allocate the corresponding memory space for their types, its accessibility and range are the same as those of dynamic variables.

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

Summary of static data members and static essentials in C.

1. static data members:
Definition:
Data members defined by the static keyword are called static data members.
Meaning:
All instances of the class to which the static data member belongs share the same static member value.
Access:
In fact, static members do not belong to an instance, and static members belong to a class. Therefore, when using static members, you must use the class name and static member name to access the instance. The reason is that static variables do not have the this pointer and can only be referenced by class names.
Memory Allocation:
Distribution time point-> the static data member is similar to the global variable, and its memory allocation is generated in the ApplicationProgramWhen the class is instantiated, that is, when the program is running, its release ends at the end of the program, the difference lies in its scope difference. Static data members only apply to this file.
Distribution location-> static variables and essentials are divided into memory allocation in static storage, rather than static memory allocation in the stack or stack.

========================================================== ==================================
2. Static essentials:
Definition:
The essentials declared using the static keyword are called static essentials.
Access:
You can also use the class name. Essentials name for access.
Brief Introduction to access rules:
A. Static essentials are not specific things. Static essentials can access static member variables, while static essentials cannot directly access instance variables.
If you access non-static members in the static essentials, the compiler will not determine whether you directly use classes or use things for misappropriation, because he cannot determine whether the non-static member is allocated memory, he is too lazy to determine.
B. If the static essentials need to access instance variables, there is only one essentials:
When Instance functions are misappropriated, instance variables can be passed as parameters to static essentials.
Source code Namespace Test
...{
Public class TT
...{
Private int num = 10;
Public void Method1 ()
...{
TT. method2 (Num); // misappropriates the instance function and reports the num parameter of the instance variable to the static essentials method2.
}
Public static void method2 (int param) // static Essentials
...{
Param = Param * Param;
Console. writeline (PARAM );
}
Public static void main ()
...{
TT. method2 (10 );
TT test = new TT ();
Test. Method1 ();
}
}
}
That is, the static essentials can accept parameters of the instance variable type, wrap the static essentials with the instance essentials, and then report the instance variables to the static essentials.
C. Static essentials cannot be used directly or indirectly. First, you must create a class instance, and then use this particular example to find the essentials.
That is, the static essentials must be used by the instance essentials. You can only use the things name and essentials name in the essentials by accepting parameters of the things type.
For example:
Class NBR
...{
// Class body;
Public void method_nbr ()
...{
// Method_nbr body;
}
}
Class MyApp
...{
Static void myapp_method (NBR object1)
...{
Object1.method _ NBR ();
}
Public static void main ()
...{
NBR myobj = new NBR ();
Myapp_method (myobj );
}
}

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.