ASP. NET private constructor Usage Analysis, asp.net private constructor

Source: Internet
Author: User
Tags class manager

ASP. NET private constructor Usage Analysis, asp.net private constructor

This document analyzes the usage of ASP. NET private constructor. Share it with you for your reference. The specific analysis is as follows:

1. Features of private constructor
 
1. Generally, constructors are not private or protected members. However, constructors can make private member functions. In some special cases, constructors are defined as private or protected members.

2. private constructor is a special instance constructor. It is usually used in a class that only contains static members. If a class has one or more private constructors without a public constructor, other classes (except Nested classes) are not allowed to create instances of the class.

3. The features of private constructor can also be used to manage object creation. Although the private constructor does not allow external methods to instantiate this class, it allows public methods (sometimes known as factory methods) in this class to create objects. That is to say, the class can create its own instance, control external access to it, and control the number of created instances.
 
 
Ii. Examples of private constructor Functions
 
1. classes with private constructor cannot be inherited
Declare a private constructor in the Animal class and let the Dog class inherit the Animal class.

Copy codeThe Code is as follows: public class Animal
{
Private Animal ()
{Console. WriteLine ("I am animal ");
}
}
Public class Dog: Animal {
}
 
Run the program and generate a solution. An error is reported, as shown in:

2. classes with private constructor cannot be instantiated.
Run the following test code:

Copy codeThe Code is as follows: class Program
{
Static void Main (string [] args)
{
Animal animal = new Animal ();
}
}
Public class Animal
{
Private Animal ()
{
Console. WriteLine ("I am animal ");
}
}
 
A solution is generated after the program runs. An error is reported, as shown in:

3. Application of private constructor

Sometimes, we do not want a class to be instantiated too much, such as global classes and routing classes. At this time, we can set constructors for classes and provide static methods.

Copy codeThe Code is as follows: public class PrivateConClass
{
Private static PrivateConClass pcc;
Private PrivateConClass ()
{
Console. WriteLine ("This private constructure function. So you cannot create an instance of this class .");
}
Public static PrivateConClass CreatePcc ()
{
Pcc = new PrivateConClass ();
Return pcc;
}
Public static void ShowStaticMethod ()
{
Console. WriteLine ("This is a static method. Just be called by Class name .");
}
Public void ShowMethod ()
{
Console. WriteLine ("This is a Nonstatic method. Just be called by private static instance pcc .");
}
}
Class Program
{
Static void Main (string [] args)
{
PrivateConClass pcc = PrivateConClass. CreatePcc ();
Pcc. ShowMethod ();
PrivateConClass. ShowStaticMethod ();
}
}

I hope this article will help you design your asp.net program.


C # usage of private constructor

A private constructor is a special instance constructor. It is usually used in a class that only contains static members. If a class has one or more private constructors without a public constructor, other classes (except Nested classes) are not allowed to create instances of the class. For example:

Class NLog
{
// Private Constructor:
Private NLog (){}

Public static double e = System. Math. 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 explicitly used to explicitly indicate that the class cannot be instantiated.

When there are no instance fields or instance methods (such as the Math class) or when a method is called to obtain an instance of the class, the private constructor can be used to block the creation of the instance of the class. If all the methods in the class are static, consider making the entire class static. For more information, see static and static class members.

Example

The following is an example of a class using a private constructor.
Public class Counter
{
Private Counter (){}
Public static int currentCount;
Public static int IncrementCount ()
{
Return ++ currentCount;
}
}

Class TestCounter
{
Static void Main ()
{
// If you uncomment the following statement, it will generate
// An error because the constructor is inaccessible:
// Counter aCounter = new Counter (); // Error

Counter. currentCount = 100;
Counter. IncrementCount ();
System. Console. WriteLine ("New count: {0}", Counter. currentCount );
}
}

Output

New count: 101

Note: If you uncomment the following statement in this example, it generates an error because the constructor is not accessible due to its protection level:
// Counter aCounter = new Counter (); // Error
================================
If you are not tired, please refer to this document or check the MSDN

C # usage of this class when the constructor is private

If all constructors of the current class are private, you can use this class using the following methods:
1. Declare a static attribute or method to the current class for use by returning the instance of the current class;
2. Set all the Members to be used in the current class to static. In this way, you can use [class name. member name] directly;

The most classic setting constructor is private. The Singleton mode is used.
Public class Manager
{
Private Manager (){}
Private static Manager _ c;
Public static Manager CurrentInstance {get {_ c = _ c ?? New Manager ();}}
}

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.