Class 02: Constructing Objects in C #

Source: Internet
Author: User

1: Accessibility of the construction method

The construction method can be either common or private.

2: Default constructor method

The default constructor method is a constructor without parameters.

If no constructor method is added for the class, a hidden default construction method is automatically generated, but the default construction method is not automatically generated if the user adds any of the constructor methods. That is, if the user adds their own defined construction method to the class, the object can only be generated using the user-defined constructor methods.

3: Constructor method call construction method

The format is as follows:

Constructor method Name (parameter list): This (parameter list)


Method body



Class derivedclass{Public DerivedClass (): this (+) {Console.WriteLine ("Derived class ' s Default constructo    R ");    } public derivedclass (int i) {Console.WriteLine ("Derived Class ' s Constructor with Param: {0}", i); }}

And the difference between Java

In Java, the constructor method calls another constructor method that uses this (parameter), but Java is placing it as a statement in the method body, and the This statement must also be the first statement of the method body.

4: In the inheritance structure, the generation of the parent class object

As with C + +, Java, if there is an inheritance structure, then the child class object will be generated as a parent class object.

By default, the default construction method of the parent class is automatically called when the subclass is constructed (all constructor methods) to produce the parent class object, so if there is no special description in the constructor method of the subclass, and the parent class does not have a default constructor, the system prompts an error.

In general, we will explicitly tell the system in the constructor method of the subclass, which constructor method of the subclass to invoke which constructor of the parent class.

The format is as follows:

Constructor method name for subclass: base (parameter list)


Method body


This allows the system to invoke the corresponding construction method of the parent class before executing this constructor method.

The difference from Java: the keyword used in Java is super, and is also placed at the beginning of the method body as a statement.

Class 02: Constructing Objects in C #

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: 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.