C # structure struct summary,
- The struct type is a value type, which is usually used to encapsulate small correlation variable groups.
- Struct can contain members: constructors, constants, fields, methods, attributes, cited objects, events, and nested types. If the preceding types are required at the same time, you should consider using classes instead of struct.
- The structure can inherit from classes (single inheritance, different from C ++) and implementation interfaces. A single structure cannot inherit from a single structure.
- The structure member cannot be declared as protected (because the structure cannot be inherited)
- Struct allocates memory space on the stack
- Struct declarative position: it is in the same level as the class or inside the class, but cannot be in the method body.
- You cannot declare a new constructor without parameters.
- Struct can declare a static constructor, but this function cannot have an access modifier and this function has no parameters.
- The static methods of non-constructor in the structure must be public in addition to the access modifier. The other methods are the same as those declared in the class.
- The struct instance statement does not need new, which is different from the reference types such as class.
Example:
Struct S {private int d = 0; public int D {get {return d;} set {d = value ;}} public S (int p) {D = p ;} public static int Sum (int a, int B) {return a + B ;}}