Asp. NET private constructor Usage Analysis _ Practical Tips

Source: Internet
Author: User

This article analyzes the usage of asp.net private constructors. Share to everyone for your reference. The specific analysis is as follows:

Characteristics of a private constructor

1. A general constructor is not a private or protected member, but a constructor can make a private member function that, on special occasions, defines a constructor as private or as a protection member.

2. A private constructor is a special instance constructor. It is typically used in classes that contain only 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 properties of a private constructor can also be used to manage the creation of objects. Although the private constructor does not allow an external method to instantiate the class, it allows the public methods (sometimes called factory methods, factory method) in this class to create objects. That is, a class can create its own instance, control the external access to it, and control the number of instances created


second, the private constructor function example explanation

1. Classes with private constructors cannot be inherited
declare a private constructor in the animal class so that the dog class inherits the animal class.

Copy Code code as follows:
public class Animal
{
Private Animal ()
{Console.WriteLine ("I am Animal");
}
}
public class Dog:animal {
}


Run the program, build the solution, and the error is shown in the following illustration:

2. Classes with private constructors cannot be instantiated
run the following test code:

Copy Code code as follows:
Class Program
{
static void Main (string[] args)
{
Animal Animal = new Animal ();
}
}
public class Animal
{
Private Animal ()
{
Console.WriteLine ("I am Animal");
}
}


After the program is run, generate the solution, and the error is shown in the following illustration:

Third, the application of private constructor function

Sometimes we don't want a class to be too instantiated, such as a global class, a routing class, and so on. At this point, we can set the constructor for the class and provide a static method.

Copy Code code as follows:
public class Privateconclass
{
private static Privateconclass PCC;
Private Privateconclass ()
{
Console.WriteLine ("This private constructure function.") So you cannot create a instance of this class. ");
}
public static Privateconclass CREATEPCC ()
{
PCC = new Privateconclass ();
return to PCC;
}
public static void Showstaticmethod ()
{
Console.WriteLine ("This are a static method.") Just is called by Class name. ");
}
public void Showmethod ()
{
Console.WriteLine ("This is a nonstatic method.") Just is 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 with the ASP.net program design.

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.