Constructors in C #

Source: Internet
Author: User

1. Private constructors

Because the constructor for the class is private, the class is instantiated in external code with the new keyword.

1  Public classStudent2 3 {4 5        Private stringname;6 7        PrivateStudent (stringName//the construction method of this class8 9        {Ten  One                This. name=name; A  - } -  the}

2. Static constructors

    • The static constructor executes only once, and the static constructor is not sure exactly when it will be executed, but it is usually executed before the member of the class is first called.
    • The static constructor does not add any access modifiers, because C # code never calls him, there is always. NET runtime calls him, so the addition of the access modifier makes no sense.
    • Static constructors do not take any parameters.
    • A class can have only one static constructor.
    • A parameterless instance constructor and a static constructor coexist safely because the execution time is different, so there is no conflict between executions.
1  Public classStudent2 3 {4 5        StaticStudent ()//Static Constructors6 7        {8 9               //CodeTen  One         } A  -}

Constructors in C #

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.