关于Structs的问题 [英] Questions about Structs
问题描述
MSDN说,一个16个字节或更少的类将被更好地处理为
为什么是这样?
这意味着如果一个结构超过16个字节,它的效率低于一个类或者是同一个?
如何确定你的类是否小于16字节?
什么限制了结构体的行为类似一个类? (除了不允许无参数构造函数)
MSDN says that a class that would be 16 bytes or less would be better handled as a struct [citation].
Why is that?
Does that mean that if a struct is over 16 bytes it's less efficient than a class or is it the same?
How do you determine if your class is under 16 bytes?
What restricts a struct from acting like a class? (besides disallowing parameterless constructors)
推荐答案
这个问题有几个不同的答案,它有点主观,原因我可以想到的是:
There are a couple different answers to this question, and it is a bit subjective, but some reasons I can think of are:
- 结构是值类型,类是引用类型。如果你使用16字节的总存储,可能不值得为每个内存引用创建内存引用(4到8字节)。
- 当你有真正小的对象,他们可以经常被推到IL堆栈上,而不是引用对象。这可以真的加快一些代码,因为你消除了在被调用者一侧的内存取消引用。
- 在IL中有一些额外的fluff与类关联,如果你的数据结构非常小,不会使用这个绒毛,所以它只是额外的垃圾,你不需要。
最重要的区别在于一个结构体和一个类之间,结构体是值类型,类是引用类型。
The most important difference between a struct and a class, though, is that structs are value type and classes are reference type.
这篇关于关于Structs的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!