C # static static data member _ Practical Tips

Source: Internet
Author: User
① static members belong to the class, and non-static members belong to instances of the class.
② every instance of creating a class, the city in memory for the non static (dynamic) State members of the new allocation of a piece of storage;

Static members are owned by the class and are common to instances of each class, regardless of how many instances the class creates, and the static members of the class occupy only the same area in memory.

Static data members are different from Dynamic data members, static variables are not stable, but static variables in the declaration regardless of whether there is no assignment, urban credential its type allocates the corresponding memory space, his accessibility and function range and dynamic variable is the same

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

A summary of static data members and static essentials in C #.

1. Static data members:
Definition:
Data members defined using the static keyword are called static data members.
Significance:
All instances of the class to which the static data member belongs share the same static member value.
Access:
In fact, static members are not part of an instance, and static members belong to a class. Therefore, when using static members, you cannot use the instance name. The static member name, instead of the class name. Static member name for that access. The reason is because static variables do not have this pointer and can only be referenced through the class name.
Memory allocation:
The dispatch point-> static data member is similar to the global variable, its memory allocation arises when the application class is instantiated, that is, when the program runs, its release is different from its scope difference. Static data members Only function in this file.
Assign locations-> static variables and essentials in static storage to distinguish between allocated memory rather than static allocation of memory on the stack or heap.

=====================================================================
2. Static Essentials:
Definition:
The essentials declared using the static keyword are called static essentials.
Access:
It is also accessed by the name of the class.
Brief introduction to access rules:
A. Static essentials are not part of a particular thing, static essentials can access static member variables, static essentials can not directly access the instance variables.
If you are accessing Non-static members in the static essentials, the compiler will not determine whether you are using the class directly or through the diversion of things, it is considered illegal, because he cannot determine if the Non-static member is allocated memory, he is too lazy to determine.
B. If the static essentials are to access instance variables, there is only one way:
The instance variable can be passed as a parameter to the static essentials in case the instance function is misappropriated.
The source code is as follows: namespace Test
... {
public class TT
... {
private int num = 10;
public void Method1 ()
... {
Tt. METHOD2 (num); instance function misappropriation, the instance variable num function parameter is communicated 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 the parameters of the instance variable type, then use the example to wrap the static essentials and then communicate the instance variables to the static essentials.
C. Static essentials can also be directly misappropriation of instance essentials, may be indirectly misappropriated, first of all to create a class instance, and then through this particular thing to misappropriate instance essentials.
That is, the static essentials to misappropriation of instance essentials, only by accepting the type of parameters, in the main body, with the name of the object. The essentials of the example.
Such as:
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.