C # constructor of Classes

Source: Internet
Author: User

Constructor is a special function used to initialize objects. The constructor starts execution during class instantiation and is used to initialize some default settings of the object.

Generally, if no constructor is provided, the compiler automatically creates a non-parameter constructor in the background. This is a very basic constructor, it can only Initialize all fields in the class to the default value of the corresponding type: for example, int type initialization 0 bool type initialization false; reference type Initialization is null.

 
Public ClassPerson {Private StringName;Private IntAge ;}
Person class

Once the constructor is provided, the compiler no longer automatically creates a default constructor:

 
Public ClassPerson {Private StringName;Private IntAge;PublicPerson (StringName,IntAge ){This. Name =Name;This. Age =Age ;}}
Constructors with Parameters

Another constructor is a static constructor which has the following features:

1. Only oneNo access modifier or ParameterStatic Constructor

2. when the class is loaded, the static constructor is executed only once, while the normal constructor starts to execute when the object is instantiated, that is to say, the static method is the first method to be executed in all constructors.

3. Static methods cannot be inherited

C # constructor of Classes

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.