Home > Net >  C # is the difference between static members and instance members
C # is the difference between static members and instance members

Time:10-12

The difference between static members and instance members c #
Static members also called Shared members, such as static attributes static field static methods; Static members can be Shared between the instances of the class,

Static class can have only static members, there can be no instance members, because of a static class cannot be instantiated;

In a static class that can have static members can also have non-static members;

As long as it is static member property field method, all need to use the name of the class to call;

The difference between static members and instance members:

Static members need by modifying the static and non-static members do not need through the static modification;

Static members belong to the class, can be directly through the way of "the name of the class. A static member access;

Non-static member, after must instantiate the object, through object. Instance members to access;

When the class is loaded for the first time (the first was loaded into memory) and the class of all static members will be loaded, how many object instance members, many objects will be created. Static members will be loaded into the static storage area, it was not until the program exits will be released;

The above clearly shows that define the static members can affect execution efficiency, so when to define static member variable?

(1) variables need to be Shared when methods need to be repeated calls when

The difference between static members and instance members:
Through the static keyword static member is needed to modify, don't use the static keyword modifiers and instance members, they distinguish the following code:

01 class Program
02 {
03 static void Main (string [] args)
04 {
05//static members belong to the class, can be directly through the way of "the name of the class. A static member" visit
06 Person. The Run ();
07//instance members belong to the object, through "object name. Instance members to access the"
08 Person p=new Person ();
09 p. ing ();
10}
11}
12 class Person
13 {
14//static member variable
15 private static int nAge;
16//instance member variables
17 private string strName;
18 public static void the Run ()
19 {
20 the Console. WriteLine (" I'll run!" );
21}
22 public void Sing ()
23 {
24 the Console. WriteLine (" I can sing ");
25}
26}
When the class is loaded for the first time (that is, the class is first loaded into memory) and the class the following all static members will be loaded, how many object instance members, how many objects will be created,
And static members are loaded into the static storage area, only is created only once, and will be released until the program exits,
Look at the code below:
01 class Program
02 {
03 static void Main (string [] args)
04 {
05 Person p=new Person ();
06 Person p1=new Person ();
07 Person p2=new Person ();
08}
09}
10
11 class Person
12 {
13//static member variable
14 private static int nAge;
15//instance member variables
16 private string strName;
17 public static void the Run ()
18 {
19 the Console. WriteLine (" I'll run!" );
20}
21 public void Sing ()
22 {
23 the Console. WriteLine (" I can sing ");
24}
25} : so what happened in memory? The diagram below:

The above clearly shows that define the static members can affect execution efficiency, so when to define static member variable?
(1) variables need to be Shared when methods need to be repeated calls when

CodePudding user response:

I am a beginner, want to ask if we can understand so,
As a System. The Console. WriteLine (); Don't I use it to new one?
Like a MessageBox. Show (); Don't I use it to new one?
So set a static, direct use,
The above is too theoretical, academic,

CodePudding user response:

Is executed without the context method, need not use internal member variables, the method of static methods can make it,
As for static member variables, are mainly in order to share the convenient,
  • Related