Differences between classes and struct

Source: Internet
Author: User
Differences between classes and struct

 1.The class and structure are very similar,   Technically speaking, class is a reference, while structure is a numerical value.   Some people say it vividly
 Class contains actions, methods, and members, which are the combination of organisms, while structure is a living organism,      
 2.In general, the class contains structure,There is Fang Farah in the class, member pulling, what drop,
 While structure only has data,     
II. Class and structure differences  
     % The class member is private by default, and the struct is public by default.  
 1. Value Type and reference type  
    
    Structure is value type: value type is allocated on the stack. All base types are structure types, such as: intCorresponding to System. int32Structure, stringCorrespondingSystem. stringStructureYou can create more value types by using the structure.  
       
 Class is a reference type: the reference type is allocated on the stack.  
         
 The execution efficiency of stacks is higher than the execution efficiency of stacks. However, the stack resources are limited and it is not suitable for processing large logical and complex objects. Therefore, structure processing is a small object to be treated as a base type, while classes process a certain business logic.  
    
    Because the structure is a value type, a value can be assigned between structures to create a new structure, while a class is a reference type. A value assignment between classes is just a copy reference.  
      
 Note:     
    
 1. Although the structure is different from the class type, their base types are all objects. in c #, all types of base types are objects.    
       
 2. Although the structure initialization also uses the NewOperator, but the structure object is still allocated on the stack rather than on the stack. If "new" (new) is not used, the field remains unassigned before all fields are initialized, and the object is unavailable.  
        
 2. Inheritance  
    
 Structure: it cannot be inherited from another structure or class. Although the structure is not explicitly declared using sealed, the structure is implicit sealed..    
    
 Class: fully scalable, unless the declared sealed is displayedOtherwise, the class can inherit other classes and interfaces, and its own can also be inherited.  
       
 Note: although the structure cannot be inheritedHowever, the structure can inherit interfaces, and the methods and class inheritance interfaces are the same.  
       
 Example: structure implementation Interface  
    
      
  
    
    3. Internal Structure:  
      
 Structure:  
      
 No default constructor exists, but you can add constructor.  No destructor   NoAbstractAndSealed (because it cannot be inherited)   There cannot be protectedModifier  You can choose not to use newInitialization  It is incorrect to initialize the instance field in the structure.     
     Class:    
      
 Default constructor available  
      
 Destructor  
    
    AvailableAbstractAndSealed    
     
 With protectedModifier  
    
    New is requiredInitialization  
      
 3. How to Select a structure or a class  
    
    After discussing the similarities and differences between structures and classes, we will discuss how to choose a structure or a class:
 
 
 1. The stack space is limited. For a large number of logical objects, creating classes is better than creating structures.
 
 
 2. structure indicates lightweight objects such as vertices, rectangles, and colors. For example, if you declare1000Array of vertex objects, the additional memory will be allocated for each referenced object. In this case, the structure cost is low.
 
 
 3. Classes are the best choice for presentation of abstract and multi-level object Layers  
    
     4. In most cases, this type is the best choice for structure when it is only some data.  
    
     
 1:The class can be inherited, and the structure cannot.
 2:The class is a reference type and the structure is a value type.
 3:Classes are in the heap, and the structure allocates memory in the stack

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.