内存分配为在.NET深继承一个类 [英] Memory allocation for a class that has deep inheritance in .NET
问题描述
如果我有类 A,B,C,D,E
,而像 X,Y,Z
和模型系统,如:
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
class E : D
如果 A
是一个抽象基类和E是类的兴趣,当我创建E的情况下,将它反过来创建<$ C $的实例C> A,B,C,D,X,Y,Z 除了电子
?
If A
is an abstract base class and E is the class of interest, when I create an instance of E, would it in turn create instances of A, B, C, D, X, Y, Z
in addition to E
?
如果是这样的话,这将创造一个巨大的性能开销?不是记忆,而是运行时间和GC明智的。
If that's the case, would this create a huge performance overhead? Not memory, but runtime and GC wise.
推荐答案
是的,这将造成A,B,C和D
嵌入式的实例
不,它不会创建X,Y和Z实例(因为它们是接口)
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)
没有为内存分配或GC(ABCD的),因为E的实例被分配为1块不需要额外的开销。任何运行时开销将完全取决于所涉及的构造。
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.
总会有一个链contructors(从E到A)执行,可能是默认的构造函数,但它也可以调用多个构造函数在1的水平。
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.
这篇关于内存分配为在.NET深继承一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!