Extracted from instance counters (C #)

Source: Internet
Author: User

I haven't published any articles for a long time, and it's also interesting. I suddenly felt some classes in C #. Why is its constructor invisible?

In addition, these classes often only have one instance in a program. So I made an experiment with the following results:

This example introduces:
Using System;

Namespace ConsoleApplication1
{
///


/// Summary of Class1.
///
Class Class1
{
///
/// Main entry point of the application.
///

[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 result:
Class2 reference: 1
Class3 reference: 1
Class3 reference: 2


After reading the above results, you may be surprised, maybe not, right? In many cases, we try

To obtain the number of instances of a class? The usual practice is instance counters. Yes. Use a static variable.

It is not appropriate to make instance counters. However, sometimes we don't want to, because we have too many instances.

For example, DataRead always has only one. So what should we do? Have you seen my experiment results above?

Ah, what have you found? Public static readonly Class2 Init = new Class2 ();

Are you interested in this sentence? Do you have any objection? Remove the static row? Well, as you wish.

Like this: public readonly Class2 Init = new Class2 (); let's see what will happen.

See. There is no way to instantiate the operation. Haha... Don't worry. Let me further explain that the static member is

In the static storage area, the configuration is completed at the initial stage of program loading. Obviously, the Init value is actually

At the beginning, an object is added to it. Then it holds a reference to this object. Let's take a look at these two sentences.

Class2 c1 = Class2.Init;

Class2 c2 = Class2.Init;

It seems that there is nothing special, right. According to my above statement, this is just to pass the reference. So instance counters

Always one, because, as I said, the configuration is completed at the beginning of the program. The following sentence is the best

Description:

// The transfer reference does not have a new object for the instance, so the instance counter is still 1
Class3 c3 = new Class3 ();
Class3 c4 = c3;
C4.refDis ();

// A new instance is allocated once, And the instance counter value is increased by 1. That is, 2
Class3 c5 = new Class3 ();
C5.refDis ();

Related Article

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.