How to choose structures or classes for instances of catalog classes and structures
One. Class and structure example comparison: Structure Example: Public Struct Person {String Name; INT Height; int Weight public bool overweight () {// Implement Something}} Sample: public class testtime {int.com; int minutes; int 2
Public void passtime () {// importation of behavior}} calling process: public class test {public static ovid main {PERSON MyPerson = New Person // Declaration Structure TestTime MyTime = New TestTime // Declaration class}} From above We can see that the statement of the statement and structure of the class is very similar, but the definition is Struct or the difference between the Class, and when using, the new structure and the new type of method are also very similar. So what is the specific difference between classes and structures?
II. Differences of class and structures 1. Value type and reference type structure are value type: Value type assigning addresses on the stack, all base types are structural types, such as: int Cord corresponds to System.String, STRING Structure, by using the structure, more value type classes are categories: Reference Types The execution efficiency of the assigned address stack on the pile is higher than the execution efficiency of the stack, but the resource of the stack is limited, not suitable for processing large logic complex Object. Therefore, structural processing as a small object treated by the base type, and the class handles a business logic because the structure is the value type, and the assignment between the structure can create a new structure, and the class is a reference type, the assignment between the classes is just a replication reference note. : 1. Although the structure is different from the type of class, their base type is object (Object), all types of base types in C # are Object 2. Although the structure is initialized, the new operator is still structural objects. Allocated on the stack instead of the pile, if you do not use "New", the field will remain unfaised before initializing all fields, and the object is not available 2. Inheritability: Cannot inherit from another structure or class, itself cannot be inherited, although the structure is not clearly declared, but the structure is implicit SeaD. Class: Fully scalable, unless the displayed declaration Sealed Other classes and interfaces can be inherited, it can also be inherited. Note: Although the structure cannot be inherited, the structure can inherit the interface, method and class inheritance interface, for example: Structure Implementation Interface IImage
{void paint ();
Struct Picture: IImage {public void Paint () {// Painting code Goes here} private int x, y, z; // taher struct members}
3. Internal Structure: Structure: There is no default constructor, but you can add constructor without the destructor without Abstract and SeaRed (because you can't inherit) You can have a protected modifier that can be initialized in the structure. The instance field is incorrect: A default constructor has a destructor that can use Abstract and Sealed with protected modifiers must be initialized using new