What's the difference between struct and class in .NET?

前端 未结 19 1613
一向
一向 2020-11-22 01:51

What\'s the difference between struct and class in .NET?

19条回答
  •  轮回少年
    2020-11-22 01:57

    +------------------------+------------------------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------------+
    |                        |                                                Struct                                                |                                               Class                                               |
    +------------------------+------------------------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------------+
    | Type                   | Value-type                                                                                           | Reference-type                                                                                    |
    | Where                  | On stack / Inline in containing type                                                                 | On Heap                                                                                           |
    | Deallocation           | Stack unwinds / containing type gets deallocated                                                     | Garbage Collected                                                                                 |
    | Arrays                 | Inline, elements are the actual instances of the value type                                          | Out of line, elements are just references to instances of the reference type residing on the heap |
    | Aldel Cost             | Cheap allocation-deallocation                                                                        | Expensive allocation-deallocation                                                                 |
    | Memory usage           | Boxed when cast to a reference type or one of the interfaces they implement,                         | No boxing-unboxing                                                                                |
    |                        | Unboxed when cast back to value type                                                                 |                                                                                                   |
    |                        | (Negative impact because boxes are objects that are allocated on the heap and are garbage-collected) |                                                                                                   |
    | Assignments            | Copy entire data                                                                                     | Copy the reference                                                                                |
    | Change to an instance  | Does not affect any of its copies                                                                    | Affect all references pointing to the instance                                                    |
    | Mutability             | Should be immutable                                                                                  | Mutable                                                                                           |
    | Population             | In some situations                                                                                   | Majority of types in a framework should be classes                                                |
    | Lifetime               | Short-lived                                                                                          | Long-lived                                                                                        |
    | Destructor             | Cannot have                                                                                          | Can have                                                                                          |
    | Inheritance            | Only from an interface                                                                               | Full support                                                                                      |
    | Polymorphism           | No                                                                                                   | Yes                                                                                               |
    | Sealed                 | Yes                                                                                                  | When have sealed keyword                                                                          |
    | Constructor            | Can not have explicit parameterless constructors                                                     | Any constructor                                                                                   |
    | Null-assignments       | When marked with nullable question mark                                                              | Yes (+ When marked with nullable question mark in C# 8+)                                          |
    | Abstract               | No                                                                                                   | When have abstract keyword                                                                        |
    | Member Access Modifiers| public, private, internal                                                                            | public, protected, internal, protected internal, private protected                                |
    +------------------------+------------------------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------------+
    

提交回复
热议问题