In .Net, when if ever should I pass structs by reference for performance reasons?

后端 未结 4 861
广开言路
广开言路 2020-12-29 07:23

In my C# application, I have a large struct (176 bytes) that is passed potentially a hundred thousand times per second to a function. This function then simply takes a point

4条回答
  •  滥情空心
    2020-12-29 07:55

    Before you ask whether or not you should pass the struct by reference, you should ask yourself why you've got such an enormous struct in the first place. Does it really need to be a struct? If you need to use a struct at some point for P/Invoke, would it be worth having a struct just for that, and then the equivalent class elsewhere?

    A struct that big is very, very unusual...

    See the Design Guidelines for Developing Class Libraries section on Choosing Between Classes and Structures for more guidance on this.

提交回复
热议问题