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

后端 未结 3 767
南旧
南旧 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:24

    Yes, it would create 'embedded' instances of A, B, C and D
    No, it would not create instances of X, Y and Z (because they are interfaces)

    There is no extra overhead for the memory allocation or GC (of ABCD) because the instance of E is allocated as 1 block. Any runtime overhead would entirely depend on the constructors involved.

    There will always be a chain of contructors (from E to A) being executed, possibly the default constructor but it's also possible to call multiple constructors at 1 level.

提交回复
热议问题