Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the structural hierarchy of the C # constructor?

Shulou Source: shulou.com Published: 2022-06-02 08:52:31 10月02日 Update

This article mainly introduces "what is the structural hierarchy of C#constructor". In daily operation, I believe many people have doubts about what is the structural hierarchy of C#constructor. I have consulted all kinds of data and sorted out simple and easy operation methods. I hope to help you answer the doubts about "what is the structural hierarchy of C#constructor"! Next, please follow the small series to learn together!

C#Constructs,Function

Constructors are special member functions that allocate storage space for objects and initialize data members.

Constructors have some special properties:

(1)The constructor must have the same name as the class;

(2)Constructor has no return type, it can take parameters or no parameters;

(3)When a class object is declared, the system automatically calls the constructor, which cannot be explicitly called;

(4)Constructors can be overloaded to provide different ways to initialize class objects;

(5)If no constructor is defined at declaration time, the system automatically generates default constructor, in which case the constructor function body is empty.

(6)Static constructor, modified with static, used to initialize static variables, a class only allows one constructor, loaded when the class is instantiated, then the modifiers public, private lose effect.

(7)You can use the public, protected, and private modifiers.

(8)When referring to parent class construction, use (): base () method, refer to self-overloaded construction using (): this (int para).

(7)You can use the public, protected, and private modifiers.

What is the hierarchy or execution order of C#constructors? Layer,Transfer,Execute

Construct objects from base classes.

publicclassMyBaseClass { publicMyBaseClass() { Console.WriteLine("InMyBaseClass()"); } publicMyBaseClass(inti) { Console.WriteLine("InMyBaseClass(inti)"); } } publicclassMyDerivedClass:MyBaseClass { publicMyDerivedClass() { Console.WriteLine("InMyDerivedClass()"); } publicMyDerivedClass(inti) { Console.WriteLine("InMyDerivedClass(inti)"); } //publicMyDerivedClass(inti,intj) //{ //Console.WriteLine("InMyDerivedClass(inti,intj)"); //} publicMyDerivedClass(inti,intj) :base(i) { Console.WriteLine("InMyDerivedClass(inti,intj):base(i)"); } } classProgram { staticvoidMain(string[]args) { //Event1 MyDerivedClassmyObj1=newMyDerivedClass(); Console.WriteLine(); //Event2 MyDerivedClassmyObj2=newMyDerivedClass(4); Console.WriteLine(); //Event3 MyDerivedClassmyObj3=newMyDerivedClass(4,8); Console.WriteLine(); Console.ReadKey(); } } Program output: InMyBaseClass() InMyDerivedClass() InMyBaseClass() InMyDerivedClass(inti) InMyBaseClass(inti) InMyDerivedClass(inti,intj):base(i)

Obviously, only when the constructor of the calling parent class is displayed with base does the program not call the constructor without parameters by default.

At this point, the study of "what is the structure hierarchy of C#constructor" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Tags: Functions caches hierarchies structures objects learning parameters methods special members more programs systems static help different practical obvious next function Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Redmi macOS MariaDB OPPO Reno