Long time not to blog, that is also fun, suddenly feeling in some C # class, why, its constructor is invisible?
And often these classes in a program there is often only one instance. So I did an experiment was as follows:
Leads by the example:
using System;
namespace ConsoleApplication1
(
/ / /
/ / / Class1 in the summary.
/ / /
class Class1
(
/ / /
/ / / Application main entry point.
/ / /
[STAThread]
static void Main (string [] args)
(
/ /
/ / TODO: Add code here to start the application
Class2 c1 = Class2.Init;
Class2 c2 = Class2.Init;
c2.refDis ();
Class3 c3 = new Class3 ();
Class3 c4 = c3;
c4.refDis ();
Class3 c5 = new Class3 ();
c5.refDis ();
Console.ReadLine ();
)
)
class Class2
(
static int ref_2 = 0;
private Class2 ()
(
+ + Ref_2;
)
public static readonly Class2 Init = new Class2 ();
public void refDis ()
(
Console.WriteLine ("Class2 reference:" + ref_2);
)
)
class Class3
(
static int ref_3 = 0;
public Class3 (){++ ref_3;)
public void refDis ()
(
Console.WriteLine ("Class3 reference:" + ref_3);
)
)
)
Output:
Class2 reference: 1
Class3 reference: 1
Class3 reference: 2
Read the above results may surprise you will find some, maybe not, is not it? In many cases, we are trying to
To get an instance of the class how many? Usually think of counter examples of the practice. Yes, with a static variable
Do instance counter is appropriate to the. But also sometimes, we do not want to, too many of our examples
Such as: DataRead it is always only one. So how can we do? I see the above experimental results?
Ah main speaker, found anything? public static readonly Class2 Init = new Class2 ();
Interested in this one is it? Do you have any objection to it? Remove the static right? Well, then, if you like.
Like this: public readonly Class2 Init = new Class2 (); let us see what happens, ah, oh
See the. No way to instantiate to operate. Ha ha. . . Do not worry, let me further explain, static members are
On the static storage area is to be completed early in the program loads configuration. So obviously, Init value, in fact,
In the beginning, the new up an object to it. It holds a reference to the object. Let us look at these two
Class2 c1 = Class2.Init;
Class2 c2 = Class2.Init;
Looks nothing special, is ah, according to the above statement, this is just in passing reference. Therefore, counter examples
Always one, because I said, is in the process of beginning to complete the configuration. So following these lines, is the best
Note:
/ / Pass the new object reference is not instance, so instance of the counter is still 1
Class3 c3 = new Class3 ();
Class3 c4 = c3;
c4.refDis ();
/ / Allocate a new instance of the first, instance of the counter value plus 1. Is 2
Class3 c5 = new Class3 ();
c5.refDis ();
Recommended links:
avchd TO mov
mp4 to Mp3 converter
Games Board Report
3DS MAX examples of lighting and LIGHTING effects
ESRI experts, non-Chan: Snow Emergency GIS can do more
Ultimate Registry Operations - Lock Registry
Q QQ Coins To Pay Part Of Wages Law, Experts Say
Learning Centers: RHCE exam on THE 30 main directions
Guide Computer Education
Tomcat For A Similar Visit To Password IIS
Police identified Results: Foxconn high fall dead before his death, Department staff
In 2010, desktop sales will reach 26.002 million
Converting mp3 to aac
Flash Game Production - Shooting
mkv video Format