Why should a .NET struct be less than 16 bytes?

前端 未结 6 1375
独厮守ぢ
独厮守ぢ 2020-11-30 07:35

I\'ve read in a few places now that the maximum instance size for a struct should be 16 bytes.

But I cannot see where that number (16) comes from.

Browsing a

6条回答
  •  情话喂你
    2020-11-30 07:48

    If a structure is not larger than 16 bytes, it can be copied with a few simple processor instructions. If it's larger, a loop is used to copy the structure.

    As long as the structure is not larger than 16 bytes, the processor has to do about the same work when copying the structure as when copying a reference. If the structure is larger, you lose the performance benefit of having s structure, and you should generally make it a class instead.

提交回复
热议问题