为什么某些类型实现为struct? [英] Why are some types implemented as struct?

查看:89
本文介绍了为什么某些类型实现为struct?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道结构和类之间的基本区别,但是

显然不足以知道为什么某些类型/概念是作为结构实现的
而大多数类型实现为类。

例如,为什么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 know the basic differences between a struct and a class, but

显然不足以知道为什么某些类型/概念是实现为struct的
而大多数类型都是作为类实现的。

例如,为什么DateTime实现为结构?为什么BitArray

作为一个类实现,但BitVector32是一个结构?

一般来说,在OO设计中,我们如何确定是否应该使用struct

还是上课?
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.


7月28日,10:33 * am,作者< gnewsgr ... @ gmail.comwrote:
On Jul 28, 10:33*am, Author <gnewsgr...@gmail.comwrote:

我知道结构和类之间的基本区别,但

显然不足以知道为什么某些类型/概念是作为结构实现的
而大多数类型都是作为类实现的。 br />
例如,为什么将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?



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


几乎每一次我已经设计了一些结构我以后发现

发现我需要能够更改它中的值并最终结果

将它转换为类,所以现在我只是不要再打扰他们了。

Nearly every time I have designed something as a struct I have later
discovered I need to be able to change the values in it and ended up
converting it to a class, so now I just don''t bother with them any more.


这篇关于为什么某些类型实现为struct?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆