Constructor in DotNet

Source: Internet
Author: User
Tags protected constructor

(1) Common Constructor
Common constructors are the most common forms that allow classes to be instantiated by users.
C # example:
1 class myClass
2 {
3 // Public Constructor
4 public myClass (){};
5}
The VB instance is as follows:
Class myClassClass myClass
{
Public Sub new () sub new ()
End sub
}

(2) private constructor
A private constructor is a special instance constructor. It is usually used in classes that only contain static members. These classes are often used as the entity class.

If a class has one or more private constructor without a public constructor, instances of the class cannot be created by other classes (except Nested classes. For example:
Class myClass
{
Private myClass (){};
Publicstatic double e = 2.71828;
}
Declare an empty constructor to Prevent Automatic Generation of default constructor. Note: if you do not use an access modifier for the constructor, it is still a private constructor by default. However, the private modifier is usually explicitly used to clearly indicate that the class cannot be instantiated.

When there is no instance field or instance method or call method to obtain the instance of the class, the private constructor can be used to prevent the instance of the class from being created.

Another application of private functions is in Singleton mode, which is the key to the implementation of this mode. The private function ensures that the instance of the class can only be implemented through the static method in the class, thus ensuring the uniqueness of the instance.
For an introduction to single-piece mode, see: http://fineboy.cnblogs.com/archive/2005/08/11/212782.html

(3) Protection of Constructors
Protected constructors enable classes to be instantiated by the quilt class, which is useless in most cases. However, this feature is used if the class can only be instantiated by the factory. The following is an example of a class that can only be instantiated by the factory:
First, we declare the constructor of the class as a protected constructor, that is, the constructor cannot be accessed except the class and subclass.

Using System;
Namespace Staticconstructor
{
Public class myNewClass
{
Protected myNewClass ()
{
}
}
}
This ensures that myNewClass cannot be instantiated directly. Then we create a factory to instantiate this class.

Using System;
Namespace Staticconstructor
{
Public class myFactory
{
Public myFactory ()
{
}
Public myNewClass CreatemyNewClass ()
{
Return new myNewClass ();
}
}
}
However, the Code cannot be compiled because the myNewClass constructor is protected and cannot be instantiated by myFactory. The result is that other classes cannot instantiate myNewClass, and myFactory cannot.

Because the constructor of myNewClass is protected, its subclass can instantiate this class. However, we cannot take the factory class as its subclass, which will lead to coupling. At the same time, we do not want to let the class itself instantiate itself, because static methods are required.

Internal classes are used to solve this problem. Create a private subclass of myNewClass in myFactory, and then obtain the instance of myNewClass through this subclass.

Using System;
Namespace Staticconstructor
{
Public class myFactory
{
Public myFactory ()
{
}
Public myNewClass CreatemyNewClass ()
{
Return new mySubClass ();
}
Private class mySubClass: myNewClass
{
Public mySubClass (): base ()
}
}
}
Of course, mySubClass actually returned by myFactory, instead of myNewClass, is no different for customers.

This method is also flawed. If a subclass of myNewClass is defined, it can be instantiated through this subclass, but this method is enough in practice.

(4) Static Constructor
A static constructor is used to initialize a class. A static constructor is automatically called to initialize the class before the first instance is created or any static member is referenced. The static constructor declaration method is as follows:
Using System;
Namespace Staticconstructor
{
Public class myNewClass
{
Static myNewClass ()
{
}
}
}
The static constructor has no modifier or parameter. It will be automatically initialized before creating the first instance or referencing any static member, and cannot directly call the static constructor.

In a program, you cannot control when to execute static constructors.

A typical purpose of a static constructor is to use this constructor to write entries to a log file when the class uses a log file. The following is an example of using a static constructor:

1 using System;
2 namespace StaticConstructor
3 {
4 public class myClass
5 {
6 public myClass ()
7 {
8 System. Console. WriteLine ("New Instance Created ");
9}
10
11 static myClass ()
12 {
13 System. Console. WriteLine ("This is static constructor ");
14}
15
16 public static void Hello ()
17 {
18 System. Console. WriteLine ("Hello ");
19}
20
21 public static void Main ()
22 {
23 myClass. Hello ();
MS = new myClass ();
25 System. Console. WriteLine ();
26}
27}
28}
29
30

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.