C # language series lecture (14)

xiaoxiao2021-03-06  74

Structure class provides support for the use of object-oriented manner, but sometimes we want to obtain a basic type of basic type like system built, and there is no inheritance. Summary data type. Customized struct types in C # provide us with this implementation, especially for building some smaller data structures. Structure and classes are very similar, they can include members such as domain, methods, attributes, events, indexes, and multiple interfaces can also be implemented. But the structure and classes have great differences, the most typical structure is the value type, and the class is a reference type. The value type makes the structural variable itself contains structural data, which are allocated on the stack, rather than being assigned to the hosted stack as a reference type such as a class. The value type is the core nature of the structure, which is derived from its various behaviors. The assignment of the structure is taken to note, look at the following code: use system; struct spoint {// point structure public int x, y; public spot (int x, int y) {this.x = x; this.y = y;}} class test {static void main () {spoint spa = new spot (10, 20); // point structure spoint spb = spa; // point structure assignment SPB.x = 0; console.writeline (Spa. X "," spa.y); // 10, 20 console.writeline (spb.x "," spb.y); // 0, 20}} The assignment of the structure causes the copy of the data within the structure, and Non-references copy of the handle! Similarly, the structure transmitted as a parameter or returned value is also a new copy of its data member. The structural example constructors and examples in the instance function are also different from the meaning of this THIS in the class. In the class, this is essentially a reference handle value, which is no longer assigned; in the structure, this is more like a variable of a structural type, which can be assigned. The default value of the structure type is to set the data member of all its value types to the corresponding value type, set the data member of the reference type to Null, and the default value of the class is NULL. Since the structural variable itself always contains its data, it is not possible to assign a structural variable to the NULL - the structure does not reference the handle of the handle, naturally, there is no NULL value! The default value rules for value types make the structure that no parameters are declared. For instance members, the structure is not allowed to initialize while the declaration. However, for static members, there is no such provision, you can declare a static constructor that implements a parametric. The declaration is not allowed to implement the destructor, which is determined by its stack allocation properties. A class variable can be embodied to the inheritance base class (such as an Object) variable, or the interface type variable thereof, which can be used, can also be reversely carried out by the interface type variables thereof or the interface type variable thereof. So how is the structural variable of a value type to be converted to an Object variable or its interface variable? C # introduces a two-way conversion to the operation of making BOX and UNBOX. Box and UNBOX operation provide a conversion bridge between the value type and the reference type, so that the data type in the C # is unified. When the structural variable needs to be converted to an Object variable or an interface variable thereof, the BOX operation is an Object variable or interface variable allocation space on the hosted stack, and copies the data variable to the corresponding position. Conversely, when the Object variable or the interface variable thereof is converted to a structural variable, the UNBOX operation enables the corresponding data from the hosted stack to the stack where the structural variable is located. Enumeration enumeration type is another lightweight value type in C #, C # uses a set of specific values ​​to express a set of specific values, such as Windows Forms Optional status, buttons control style, etc.

转载请注明原文地址:https://www.9cbs.com/read-121266.html

New Post(0)