Memory allocation for a class that has deep inheritance in .NET

后端 未结 3 725
南旧
南旧 2021-02-10 02:26

If I have classes A, B, C, D, E, and interfaces like X, Y, Z, and model a system like:

class B : A, X
class C : B, Y
class D : C, Z
cla         


        
3条回答
  •  北荒
    北荒 (楼主)
    2021-02-10 03:07

    It would create a single object - an instance of E - but that would include all the fields declared in the class hierarchy. (Interfaces can't declare fields, so they're irrelevant to the data within the object itself.) It's only fields (and any attributes affecting layout, admittedly) that contribute to the memory taken up by an object.

    The reference to the instance of E could be "converted" to a reference of type A, B, C, D, X, Y or Z as an identity-preserving reference conversion - i.e. it would still be a reference to the same object.

提交回复
热议问题