Differences between structural and classes in C #
table of Contents
Comparison of examples and structures
Differences between classes and structures
How to choose a structure or class
One. Examples of class and structure comparison:
Structure Example:
Public struct person
{
String name;
INT height;
Int weight
Public bool overweight ()
{
// Implement Something
}
}
Class example:
Public Class TestTime
{
INT HOURS;
Int minutes;
INT Seconds;
Public void passtime ()
{
//Mplementation of Behavior
}
}
Calling process:
Public Class Test
{
Public Static Ovid Main
{
Person myperson = new person // declared structure
TestTime MyTime = New TestTime // Statement class
}
}
From the above example we can see that the statement of the statement and structure is very similar, but the definition is Struct or the Class of the Class, and when used, the new structure and the new type of methods are also very similar. So what is the specific difference between classes and structures?
II. Differences between classes and structures
1. Value type and reference type
Structure is a value type: Value type assigns an address on the stack, all of the base types are structural types, such as: INT corresponds to the System.InT32 structure, String corresponds to the system.string structure, and more value types can be created by using the structure
Class is a reference type: reference type assigns an address on the heap
The implementation efficiency of the stack is higher than the implementation efficiency of the stack, but the resource of the stack is limited, and it is not suitable for processing large logic complex objects. 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, the assignment between the structure can create a new structure, and the class is a reference type, and the assignment between the classes is just a replication reference.
Note:
1. Although the structure is different from the type of class, but their base type is object (Object), all types of base types in C # are Object.
2. Although the initialization of the structure also uses the New operator, the structural object is still allocated on the stack instead of the pile. If you do not use "New", the field will remain unfaised before initializing all fields, and Object is not available
2. Inheritance
Structure: It cannot be inherited from another structure or class, it cannot be inherited itself, although the structure is not clearly declared, but the structure is implicit SeaD.
Category: Fully scalable unless the displayed declaration Seate, you can inherit other classes and interfaces, 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
Interface IImage {void Paint ();} struct picture: iimage {public void point () {// painting code goes here} private int x, y, z; // other struct members}
3. Internal structure:
structure:
There is no default constructor, but can add constructor to add
No destructor
No Abstract and SeaD (because you can't inherit)
No protected modifier
Can not initialize new
The initialization instance field in the structure is wrong
class:
Default constructor
Have a patterned function
You can use Abstract and Sealed
Protected modifier
I must use new initialization
three. How to choose a structure or class
After discussing the same and differences between the structure and classes, the following discusses how to choose to use the structure or class:
1. The stack is limited, for a large number of logical objects, the creation class is better than the creation structure.
2. The structure represents a lightweight object such as points, rectangles, and colors, for example, if a array containing 1000 point objects is declared, the additional memory is assigned to each object. In this case, the cost of the structure is low.
3. The class is the best choice when the abstract and multi-level object hierarchy
4. In most cases this type is just some data, the best choice is