C #, Class and Struct difference

C #, Class and Struct difference

   

1) class is a reference type, inherited from System.Object; stuce are value types System.ValueType class inherits from, and therefore does not have the polymorphism. Note, however, System.ValueType is a reference type.
2) from a functional point of view, class performance behavior; and stuct often used to store data.
. 3) to support class inheritance, and interfaces can inherit from classes; inheritance without the struct, struct not inherit from the class, the base class can not be used as a class, but supports interface inheritance struct.
4) classs can declare no argument constructor, destructor can be declared; struct only declared parameters and the constructor and destructor can not be declared. Thus, struct no custom default constructor without parameters, a default constructor with no arguments is simply the value of all of its initial value of 0 and the like.
5) instantiation, to use the new keyword class; struct and may not use the new keyword, for instance when struct If no parameters can not new; but if there is a constructor parameter generation, it is necessary with the new. If you do not come in a new instance of the struct, then all of its fields will be unassigned until all fields are initialized, otherwise unassigned reference field causes a compilation error.


6) class may be an abstract class (abstract), you may declare abstract function; struct abstract and can not be declared abstract function.
7) class can be declared protected members, virtual members, sealed members and override members; but not the struct Notably, struct can override System.Object 3 virtual method, (Equals (), Tostring ( ), GetHashTable ())
. 8) class objects are copied into the shallow vs. deep copy, copy must be accomplished through a special method; struct created and the object a simple copy may be directly connected to an equal sign.
9) class instance by a garbage collection process to ensure recovery of the memory; struct variables and automatically deallocated immediately after.
10) passed as a parameter, class variables passed in Anzhi manner; struct and by variables are passed by value.

We can easily understand, is a class of machines can move, with behavioral, multiple state; struct the box is a part, combining parts of different structures.
Of course, the use of class basic alternative to any occasion struct can be said in object-oriented programming, the rampant world. class come from behind. But some do not understand This is useful with struct.

a) When implementing a custom configuration for storing data can be considered struct.
b) struct stack space occupancy variable, only suitable where a relatively small amount of data.
c) an array of structures having a higher efficiency.
d) providing compatibility with certain communication and unmanaged code

Released three original articles · won praise 0 · Views 64

Guess you like

Origin blog.csdn.net/dhsajkd/article/details/104077552