In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to construct functions in C#". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
C# constructors are generally divided into static C# constructors and instance C# constructors. Of course, the instance C# constructor is executed when the class is instantiated, which is easy to understand, while the static C# constructor is something special:
The static C # constructor is executed only once. In C #, the static C # constructor usually executes before the member of the secondary call class. One reason for writing a static C# constructor for a class is that the class has some static fields or properties that need to be initialized from an external source before * using the class. The .NET runtime does not ensure that the static C# constructor is executed, but runs at most once.
Static C # constructor can not take any parameters, there is no access modifier, other C # code never calls it is always called by the C# runtime, so the modifier does not make any sense, a class can only have one static C # constructor, static C # constructor can only access static members of the class, not instance members
If a C # constructor with parameters is provided, the compiler does not automatically provide a default C # constructor, but only if no C # constructor is defined.
The static C# constructor executes when the class is loaded, while the instance C# constructor executes when an instance of the class is created, so it can safely coexist in the class, even if the parameter list is the same.
(2) call the C# constructor from other C# constructors
Sometimes there are several C # constructors in a class to accommodate some optional parameters, which contain some common code, for example:
Class car... {private string description; private unit wheels; public Car (string model,unit nWheels)... {this.desciption=description; this.nWheels=nWheels;} public car (string model)... {this.description=description; this.nWheels=4;}}
The two C # constructors in the above class initialize the same field. C # has a special syntax that squares all the above code together, saying that the C # constructor initializer can be implemented:
Class car... {private string description; private unit wheels; public Car (string model,unit nWheels)... {this.desciption=description; this.nWheels=nWheels;} public car (string model): this (model,4)... {}}
The above this keyword only calls the C # constructor with the most matching parameters. Note here that the construction initializer executes before the C # constructor!
Suppose you run the following code:
Car myCar=new Car (\ "Proton Persona\"); "how to construct functions in C#" is introduced here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.