更新时间:2023-02-15 20:14:11
(更新,感谢捐款来自其他用户)
(updated, thanks to contributions from other users)
不同于类,结构是在堆栈中创建。因此,它是更快实例(和销毁)一个结构不是一个类。
Unlike class, struct is created on stack. So, it is faster to instantiate (and destroy) a struct than a class.
除非(亚当·罗宾逊指出的)结构是在这种情况下,它被分配在堆中的类成员,以及其他一切。
Unless (as Adam Robinson pointed out) struct is a class member in which case it is allocated in heap, along with everything else.
在另一方面,每次分配结构或将它传递给一个函数的时候,它就会被复制。
On the other hand, every time you assign a structure or pass it to a function, it gets copied.
我不认为有一个结构大小的硬性限制。千字节肯定是太多了。 MSDN 说:
I don't think there's a hard limit for a struct size. Thousands of bytes is definitely too much. MSDN says:
除非你需要引用类型 语义,一类是小 大于16字节可以更有效地 通过该系统处理为一个结构。
Unless you need reference type semantics, a class that is smaller than 16 bytes may be more efficiently handled by the system as a struct.
这是 - 如果你需要按引用传递这使它成为一个类,无论大小击>
在第二个想法,你仍然可以通过结构由简单地引用在函数参数列表中指定裁判。
On the second thought, you can still pass struct by reference simply by specifying ref in the function parameter list.
所以,大的结构实际上是确定的,如果你通过引用传递,并作为类成员使用。
So, a large struct can actually be ok if you pass it by reference and use as a class member.