What is the benefit of using out/ref versus returning?

前端 未结 2 860
庸人自扰
庸人自扰 2021-01-17 20:03

I\'m making a game using XNA framework, so I use a lot functions that operate on vectors. (especially Vector2 (64bit struct)). What bothers me is that most of the methods ar

相关标签:
2条回答
  • 2021-01-17 20:26

    Vector2 is a struct, which means that when it's returned as a value a copy is returned, rather than returning a reference to an existing structure. By using ref/out parameters you can avoid this copy so that the Vector created in the Min method is the exact vector in your result variable.

    It's one of those micro optimization that normally would be discouraged, but in the game world it's done often enough, and in environments where performance matters enough, that it's worth the slightly less readable option.

    0 讨论(0)
  • 2021-01-17 20:33

    Another difference on top of the performance efficience mentioned by Servy is the ability to have multiple "return" values: instead of returning them the usual way, you list them as ref/var parameters.

    0 讨论(0)
提交回复
热议问题