Thursday, October 14, 2010

Leads by the example of the counter (C #)


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:



Comment Inventory And Barcoding



Holiday promotion: how to make the cost may change from control



the easiest way to Uninstall ati drivers use ati



Ali power to help her mother five years beyond the traditional media blog



Create UNIX Backdoor (primary chapter)



Zhongguancun hand new countryside



Informix easy to understand and explain the usage of common commands



AVI to FLV



MPEG to 3GPP



Picked Astrology Or Biorhythms Or Mystic



I Have A A4 Paper Give You A High Performance [1]



DivX To IPhone



PhotoShop new tricks: Paintings in paintings



0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home