Differences between structures and classes in C #

Source: Internet
Author: User

Comparison of classes and structures

  • Structure example

    Public struct person

    {

    String name;

    Int height;

    Int weight

    Public bool overweight ()

    {

    // Implement something

    } S

    }

    Class Example

    Public class testtime

    {

    Int hours;

    Int minutes;

    Int seconds;

    Public void passtime ()

    {

    // Implementation of behavior

    }

    }

    Call Process

    Public class test

    {

    Public static Ovid main

    {

    Person myperson = new person // declaration Structure

    Testtime mytime = new testtime // Declaration class

    }

    }

    From the above example, we can see that the declaration of the class and the declaration of the structure are very similar, but the difference between struct and class is after the qualifier, and when used, similar methods are used to define new structures and new classes. What are the specific differences between classes and structures?

    Differences between classes and structures

    Value Type and reference type

    Structure

    The structure is a value type, and the value type is allocated to the stack. All base types are structure types. For example, int corresponds to system. int32 structure. String corresponds to system. string Structure. You can create more value types by using the structure.

    Class

    Class is the reference type, and 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 treated as a base type, and classes process a business logic.

    Because the structure is a value type, you can create a new structure by assigning values between structures. Classes are reference types, and assigning values between classes is just copying references.

    Note:

  • Although the structure and class types are different, their base types are all objects, and all types of base types in C # Are objects.

  • Although the new operator is used for structure initialization, the structure object is still allocated on the stack instead of the stack. If the new operator is not used, before all fields are initialized, the field remains unassigned and the object is unavailable.

  • 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 displayed, the class can inherit other classes and interfaces, and its own can also be inherited.

    Note:

  • Although the structure cannot be inherited, the structure can inherit interfaces. methods are the same as class inheritance interfaces.

    Example: structure implementation Interface

    Interface iimage

    {

    Void paint ();

    }

    Struct picture: iimage

    {

    Public void paint ()

    {

    // Painting code goes here

    }

    Private int x, y, z;

    // Other struct members

    }

    • internal structure

      structure

    • no default constructor exists, however, you can add constructors

    • NO destructor

    • no abstract and sealed (because it cannot be inherited)

    • the protected modifier cannot exist

    • New Initialization is not allowed.

    • initializing the instance field in the structure is incorrect

    • class

    • default constructor

    • destructor

    • abstract and sealed can be used

    • protected modifier

    • New Initialization is required.

    • how to choose a structure or a class

      after discussing the similarities and differences between structures and classes, the following describes how to select a structure or a class:

    • the stack space is limited. For a large number of logical objects, creating a class is better than creating a structure.

    • structure indicates lightweight objects such as vertices, rectangles, and colors, for example, if an array containing 1000 vertex objects is declared, additional memory will be allocated to each referenced object. In this case, the structure cost is low.

    • when presenting abstract and multi-level object layers, class is the best choice.

    • in most cases, structure.

    • ----

      answers from csdn:

      the structure can be considered as a lightweight class, which is better in performance.

      similarities:

    • structures and classes are pointer-oriented for Programs .

    • Differences:

    • structured objects are always operated on the thread stack rather than hosted stacks.

    • A struct cannot be inherited. Therefore, you do not need to query the vtable when calling the struct method: the virtual function inherits the table.

    • we cannot declare a struct with an empty Constructor (I don't know why we have to design it like this)

    • all variables must be initialized in the constructor of the struct (I don't know why I have to design it like this)

    • struct fields cannot have default values (both are binary values by default ), however, you can change the "Default Value" in the constructor

    • .....

      According to the meaning on msdn, It is very small in practice to use struct. The structure guide:

    • The behavior is the same as the primitive type.

    • Instance size smaller than 16 bytes.

    • It cannot be changed.

    • Value semantics is desirable.

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.