In fact, before introducing Struct, we should conduct a preliminary study on the principle of classes (CLASS). However, from another aspect, we can also explore this structure with a blood relationship to obtain further awareness of classes. First, the structure of the C # is a significant language function. Like the class, the structure can also contain other types. Since the structure is internally value type, the structure is sometimes referred to as a light version of the class. At the same time, the structure does not assume the overhead brought by the reference object, unless the exception is provided. The structure also has an important limit, so he can only be used in very special occasions. The following discuss their limitations and his advantages. The definition of the structure and the definition of the class are basically consistent: [Attributes] [Modifiers] Struct
[: interfaces]
{
[struct-body]
} [;]
For the sake of understanding, I will give an example to discuss. We first define a structure that describes specific things - "611311 classes".
Struct stuclass {
Public String ClassName;
Public Object Classmumber;
Public int classnumber;
}
So far, this statement is like a class. However, you will see many of the restrictions on the use structure.
Customers do not have to instantiate structures (via new keyword). This is because, as a value type, once the structure is declared, it is assigned.
However, it is this usage, so if we do not personally explicitly initialize the structure (using the new keyword), the field will not be initialized. As such code, compile will be wrong:
Stuclass S611311;
Console.writeline (s611311.classname);
The following code will correct this error. Note that because s611311.classname is a value type, the initialization mold is considered 0;
Stuclass s611311 = new stuclass ();
Console.writeline (s611311.classname);
The structural members I know include constructors, constants, fields, methods, features, indexers, operators, and other types. However, in terms of constructors, the structure has a very important limit: the unable to create a parameterless constructor.
So, the following code cannot be compiled:
Struct stuclass {
Public stuclass ();
Public String ClassName;
Public Object Classmember;
Public int classnumber;
}
However, we can define the prototypes with parameters:
Struct stuclass {
Public Stuclass (String Name, Object MeNber, int number)
{
Classname = name;
Classmember = menet;
ClassNumber = Number;
}
Public String ClassName;
Public Object Classmember;
Public int classnumber;
}
If you carefully study the grammar of the defined structure, you will notice that there is no list of the base class. This is because the structure cannot be based on other structures or classes, and they cannot be found to be other structures or classes. If you want to see more intuitive conclusions, you'd better define a class and structure with the same members in the .NET development environment, then go to the MSIL generated by the compiler.
From this, we can conclude: 1. The definition of the structure is closed (cannot be used as a base class); class). 3, the structure does not have the default constructor.
(I want to know the principles and routines of the use of the structure, and look after the next time ...)
Here, I will then introduce the principle that should pay attention to and grasp when using the structure: Through the introduction of the above, we can naturally realize the superiority of the structure in efficiency (relative to class), which is mainly due to their underlying Value type structure. However, their limitations exhibited when the algorithm for high-capacity data and high complexity is made, making it a limited range. So, where are we use the structure, can you not be accused and ridicule? 1. If you are engaged in image color or fixed mode business handler design, or when you design the object group, you need to face an object that is simpler and less structure, and the status information is relatively small, I suggest you choose the structure type. To construct these small-scale data bodies. 2. Since the prototype of the structure is the value type, it is not a data that is defined as a data, so do not try to construct excessive methods in the structure, it is best not to define methods to avoid it. Let's see a most representative example provided by Microsoft: RGB Structure Using System; ///
Author Blog: http://blog.9cbs.net/heiding/