C # collection series (3): Constructor

Source: Internet
Author: User

 

1. Demonstrate the inheritance of Constructors

    • Declare an empty constructor to Prevent Automatic Generation of default constructor.
    • If you do not use an access modifier for the constructor, it is still a private constructor by default.
    • The private modifier is explicitly used to explicitly indicate that the class cannot be instantiated.

 

2. Static Constructor

    • Static constructors have the following features:
    • The static constructor neither has an access modifier nor a parameter.
    • Before creating the first instance or referencing any static member, the class is automatically initialized by calling the static constructor.
    • Static constructor cannot be called directly.
    • InProgramThe user cannot control when to execute the static constructor.
    • A typical use of a static constructor is to use this constructor to write entries to a log file when the class uses a log file.
    • Static constructors are not hostedCodeIt is also useful when creating a packaging class.

3. Example:

 

UsingSystem;

UsingSystem. Collections. Generic;

UsingSystem. LINQ;

UsingSystem. text;

 

// Description: test the constructor.

 

NamespaceNettest

{

Public Class Father

{

 

PublicFather ()

{

Console. Out. writeline ("Father say");

}

Public static father ()

{}

*/

 

PublicFather (StringS)

{

Console. Out. writeline (S +": Father say");

}

}

 

Public Class Son:Father

{

/*

// Call the class constructor of the parent constructor without Parameters

Public son ()

{

Console. Out. writeline ("son say ");

}

*/

PublicSon (StringS)

{

Console. Out. writeline ("Good son say :");

}

 

/* Base display calls to the parent class Constructor

Public son ()

: Base ("Jack ")

{

Console. Out. writeline ("son say ");

}

*/

 

/* The subclass implicitly calls the non-argument constructor of the parent class */

/* This calls its own overloaded constructor */

PublicSon ()

:This("Call myself another constructor")

{

Console. Out. writeline ("Above sentence calling myself");

}

}

 

Public Class Testconstructor

{

 

Public VoidTest ()

{

New Son();

}

}

}

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.