C # Foundation--Private constructor function in class

Source: Internet
Author: User
Tags natural logarithm

if a class member has a private modifier, it is not allowed to access the class member outside of the class scope. When you apply the private modifier to a class constructor, the outer class is prevented from creating an instance of the class. Although it seems a bit difficult to understand (since it cannot be instantiated, what is the use of this class?), but in fact this is a very powerful feature.

Most obviously, a private constructor is often used if a class only provides functionality through static methods and fields. the System.Math class in the Framework class library FCL is a classic example.

The System.Math class has two static fields: Pi and E (natural logarithm base), and some methods for returning trigonometric values. These methods are used as built-in functions, so it is not necessary for the program to create an instance of the math class for using these fields and methods.

It may be natural to have a question like this: Do you want to avoid instantiation, use a private constructor, or use an abstract class? The answer is to understand the difference. first, consider inheritance, although an abstract class cannot be instantiated, but its true purpose is to be used as a base class so that derived classes (which can be instantiated) create their own implementations. Classes that use private constructors are not inherited and cannot be inherited. Second, a private constructor can only prohibit an external class from instantiating the class, but it cannot prohibit the creation of instances within that class.

the properties of private constructors can also be used to manage the creation of objects. Although the private constructor does not allow external methods to instantiate this class, it allows public methods (sometimes called factory methods, factory method) in this class to create objects. In other words, a class can create its own instance, control access to it, and control the number of instances created

Note: This article is from 51cto.com

C # Foundation--Private constructor function in class

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.