问题描述
我知道结构和类之间的基本区别,但是
显然不足以知道为什么某些类型/概念是作为结构实现的
而大多数类型实现为类。
例如,为什么DateTime实现为结构?为什么BitArray
作为一个类实现,但BitVector32是一个结构?
一般来说,在OO设计中,我们如何确定是否应该使用struct
或类?
I know the basic differences between a struct and a class, but
apparently not good enough to know why some types/concepts are
implemented as struct whereas most types are implemented as class.
For example, why is DateTime implemented as a struct? Why is BitArray
implemented as a class, but BitVector32 a struct?
In general, in OO design, how do we determine if we should use struct
or class?
推荐答案
我认为,一般来说,结构适用于使用固定内存量的小东西。
I think that, in general, structs are for small things that use a fixed
amount of memory.
Struct被放置在堆栈的内存中,因此更快且不需要使用垃圾回收来清理
。结构不能延长,也不能扩展其他任何东西。
类在堆上,必须清理并可以扩展或
延伸。
对于我们大多数人来说,一个大的那么什么?
我假设选择结构vs class是MS
在框架中认为比公共代码grunt更多的东西和
它首先归结为性能,然后是代码中的Symmetry 。
这就是我对它的看法。
Tal
Struct are placed in memory on the stack, therefore faster and do not
need to be cleaned up with garbage collection. Structs cannot be
extended and cannot extend anything else.
Classes Are on the heap, must be cleaned up and can be extended or
extend.
For most of us, a big "So what?"
I am assuming the choice af struct vs class is something that MS
thinks a lot more about in a framework than the common code grunt and
it comes down to first, performance and then to Symmetry in the code.
thats my take on it.
Tal
这篇关于为什么某些类型实现为struct?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!