Usually in programming rarely used to struct, today in the process of writing an algorithm need to use, so to check the net, found that there is a lot of things in the struct I do not know, then I will check the information recorded down with everyone to share.
The first structure is the value type.
Structs are defined using the struct keyword, and the structure is as follows:
struct structure name
{
}
Structure Overview
The structure has the following characteristics:
A struct is a value type, and a class is a reference type. (A struct cannot contain an explicit parameterless constructor)
Unlike classes, structs can be instantiated without the use of the new operator.
Structs can declare constructors, but they must take parameters.
A struct cannot inherit from another struct or class and cannot be used as a base for a class. All structures are directly inherited from System.ValueType, which inherit from System.Object.
Structures can implement interfaces.
Structs cannot be given an initial value when defining a variable. Plus
If you want to use a constructor in a struct, you must assign a value to all variables (assign a value to the variable directly in the constructor instead of assigning a value to the variable's property, because the property has no value before it is assigned, so it cannot be given directly to the property) (plus)
All structures are implicitly inherited from the ValueType class and do not need to display the specified;. Plus
Only interfaces are allowed in the inheritance list of structs. Plus
The inheritance of the structure is three layers: Object >> valuetype >> "Structure". Plus
For example:
1 structemployeestruct2 {3 Private stringname;4 Public stringName5 {6 Get{returnname;}7 Set{name =value;}8 }9 Ten Private intAge ; One Public int Age A { - Get{returnAge ;} - Set{age =value;} the } - - Private intGongzhi; - Public intGongzhi + { - Get{returnGongzhi;} + //set {Gongzhi = value;} A } at //Parametric Constructors - PublicEmployeestruct (string_name,int_age,int_gongzhi) - { - //If you want to use constructors in structs, you must assign values to all variables (assign values in constructors) - This. Name =_name; - This. Age =_age; in This. Gongzhi =_gongzhi; - } to } + - //working with Structures the * class Program $ {Panax Notoginseng Static voidMain (string[] args) - { the + ////Instantiate employeestruct structure A //employeestruct empstruct = new Employeestruct ("Steven", +, 10000); the //Console.WriteLine ("Name: {0}\n Age: {1}\n salary: {2:c2}", Empstruct. Name, Empstruct. Age, Empstruct. Gongzhi); + Console.read (); - } $}
working with Structures
struct types are suitable for light-weight objects such as point, Rectangle, and Color. Although you can represent a point as a class, in some cases it is more efficient to use the structure. For example, if you declare an array of 1000 point objects, you need to allocate more memory in order to reference each object, in which case you can conserve resources by using structs. Because the. NET Framework contains an object named point, we call the struct "CoOrds" instead.
1 Public structCoOrds2 {3 Public intx, y;4 PublicCoOrds (intP1,intp2)5 {6x =P1;7y =P2;8 }9}
The default (parameterless) constructor for declaring a struct is wrong. Default constructors are always provided to initialize struct members to their default values. Initializing an instance field in a struct is also an error.
If you create a struct object using the new operator, the structure object is created and the appropriate constructor is called. Unlike classes, structs can be instantiated without the use of the new operator. If you do not use new, the field remains unassigned and the object is not available until all fields are initialized.
For structs, inheritance does not exist like a class. A struct cannot inherit from another struct or class and cannot be used as a base for a class. However, structs inherit from the base class Object. Structs can implement interfaces in exactly the same way.
Unlike C + +, classes cannot be declared with the struct keyword. In C #, classes and structs are semantically different. A struct is a value type, and a class is a reference type. For more information, see Value types.
Unless you need to reference type semantics, the system will be more efficient at processing smaller classes as structs.
Example 1:
The following example shows struct initialization using the default constructor and parameterized constructors.
1 Public structCoOrds2 {3 Public intx, y;4 PublicCoOrds (intP1,intp2)5 {6x =P1;7y =P2;8 }9 }Ten //Declare and initialize struct objects. One classTestcoords A { - Static voidMain () - { the //Initialize: -CoOrds COORDS1 =NewCoOrds (); -CoOrds COORDS2 =NewCoOrds (Ten,Ten); - + //Display Results: -System.Console.Write ("CoOrds 1:"); +System.Console.WriteLine ("x = {0}, y = {1}", coords1.x, coords1.y); A atSystem.Console.Write ("CoOrds 2:"); -System.Console.WriteLine ("x = {0}, y = {1}", coords2.x, coords2.y); - } -}
Output
CoOrds 1:x = 0, y = 0
CoOrds 2:x = ten, y = 10
Example 2:
Here is an example of a feature that is unique to a structure. It creates a CoOrds object without using the new operator. If you change the struct to class, the program will not compile.
1 Public structCoOrds2 {3 Public intx, y;4 PublicCoOrds (intP1,intp2)5 {6x =P1;7y =P2;8 }9 }Ten One //Declare a struct object without "new." A classtestcoordsnonew - { - Static voidMain () the { - //Declare an object: - CoOrds coords1; - + //Initialize: -coords1.x =Ten; +COORDS1.Y = -; A at //Display Results: -System.Console.Write ("CoOrds 1:"); -System.Console.WriteLine ("x = {0}, y = {1}", coords1.x, coords1.y); - } -}
Output
CoOrds 1:x = ten, y = 20
struct in C #