Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the structural hierarchy of the C # constructor?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report