In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to construct a function in C #". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to construct a function in C#"!
Constructors in CLR Via C # are a special way to initialize an instance of a type to a valid state. Constructors are usually represented by .ctor in metadata, as you can see in IL code. When creating an instance of a type, it is usually divided into three steps:
1 allocate memory for the data fields of the instance.
2 create object pointers and synchronization index blocks.
3 call the instance constructor of the type to set the initial state of the object.
Instance constructor for reference types in CLR Via C#
Before creating an object of a reference type, the memory allocated by the object is cleared before calling the instance constructor of the type, that is, all fields that do not show assignments in the constructor will be set to 0 or null.
Unlike the general method, the instance constructor can never be inherited, and all the following keywords cannot be used for the instance constructor (virtual new override sealed abstract).
If no constructor definition is shown in a class, the C # compiler defines a default no-parameter constructor.
The access modifier for the default constructor of the abstract class is protected.
The constructor can initialize fields, but in the c # language it provides an easy way to assign values directly to initialize the fields when they are defined. As follows:
Public class User2 {private int _ age = 25; private string _ name = "oec2003";}
It's really convenient to be like above, but if there are several initialized instance fields and multiple overloaded constructors at the same time, you should put the initialization of the instance fields into a common constructor, and the other constructors display calls to the constructor through this, which reduces the size of code generation, as shown in the following example.
Public abstract class User {private int _ age=25; private string _ name= "oec2003"; private string _ email = "oec2003@gmail.com"; public User (Int32 age) {this._age = age;} public User (string name) {this._name = name;} public User (Int32 age, String name, String email) {this._age = age This._name = name; this._email = email;}
The correct way to write it should be like this
Public abstract class User {private int _ age; private string _ name; private string _ email; public User () {_ age=25; _ name= "oec2003"; _ email = "oec2003@gmail.com";} public User (Int32 age): this () {this._age = age } public User (string name): this () {this._name = name;} public User (Int32 age, String name, String mail): this () {this._age = age; this._name = name; this._email = email;}}
Instance constructor of value type in CLR Via C#
The instance constructor of a value type is very different from that of a reference type. A constructor with no parameters cannot be included in a value type, and a compilation error will occur if you explicitly specify a constructor without parameters. A compilation error occurs in the following code:
Struct User {public Int32 _ age; public String _ name; public User () {_ age = 25; _ name = "oec2003";}}
The value type cannot contain a constructor without arguments, the field cannot be initialized in the value type, and the following code will not be compiled.
Public struct User {public Int32 _ age=25; public String _ name= "oec2003";}
You can also have a constructor in a value type, but the constructor must contain parameters and initialize all fields. Constructors that contain parameters but do not initialize all fields cannot be compiled. Look at the following code: it can be seen that all fields must be initialized if the value type contains the constructor.
If there are multiple constructors, each constructor must also ensure that all fields are initialized, otherwise it cannot be compiled. If the value type does not contain a constructor, all fields are set to 0 or null when instantiated.
Public struct User {public Int32 _ age; public String _ name; / / only initializes _ age public User (Int32 age) {_ age = age;} public User (Int32 age,String name) {_ age = age; _ name = name;}}
Type constructor in CLR Via C #
Type constructors are also called static constructors. Static constructors can be used to reference types and value types. Unlike instance constructors, static constructors always have only one in a type and cannot contain parameters. Only static fields can be initialized in a static constructor.
The following code shows static constructors in value types (unlike strength constructors, where static constructors that define no parameters are allowed to be displayed) and reference types, respectively.
/ / value type public struct User {public static Int32 _ age; public static String _ name; static User () {_ age = 25; _ name = "oec2003";}} / / reference type public class User {public static Int32 _ age; public static String _ name; static User () {_ age = 25 _ name = "oec2003";}}
To prevent code written by developers from calling static constructors, the C # compiler defines static constructors as private and cannot explicitly add access modifiers to static constructors, which will result in a compilation error.
As mentioned above, you cannot assign a value to an instance field in a value type, otherwise a compilation error will occur, but you can assign a value to a static field at definition time, as shown in the following code:
Public struct User {public static Int32 _ age = 25; / / static field public String _ name = "oec2003" can be initialized correctly; / / error cannot initial instance field}
The static constructor in CLR Via C # should not call the static constructor of the base class, because static fields are not inherited into subclasses, so this makes no sense.
At this point, I believe you have a deeper understanding of "how to construct a function in C#". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.